enum rawValue 타입 에러

김태현·2022년 2월 18일
0

swift

목록 보기
7/7
post-thumbnail

enum 원시 타입으로 다른 enum 타입을 사용하면 문제 발생

enum GameResult {
    case win
    case loss
    case tie
}

enum Game: GameResult {
    case 가위바위보 = GameResult.win
    case 묵찌빠 = GameResult.tie
}
enum GameResult: String {
    case win = "win"
    case loss
    case tie
}

enum Game: GameResult {
    case 가위바위보 = GameResult.win
    case 묵찌빠 = GameResult.tie
}

위 두 코드에서의 에러 문구

  1. 'Game' declares raw type 'GameResult', but does not conform to RawRepresentable and conformance could not be synthesized
  2. Do you want to add protocol stubs?
  3. Raw type 'GameResult' is not expressible by a string, integer, or floating-point literal
  4. Raw value for enum case must be a literal

⇒ 원시값은 literal만 가능하다.

⇒ literal이 무엇인가? = String or Int or Floating-point

⇒enum 타입의 case의 원시값을 리터럴 할당한다고 해당 enum 타입이 리터럴로 표현된다고 할 수 없다?

String or Int or Floating-point가 아니라 enum 타입이라서?


리터럴

Swift Literals

A literal is a representation of a value in source code, such as a number or a string. Swift provides the following kinds of literals:

profile
iOS 공부 중

0개의 댓글