Swift enum with custom initializer loses rawValue initializer

后端 未结 6 1481
长发绾君心
长发绾君心 2021-01-30 12:07

I have tried to boil this issue down to its simplest form with the following.

Setup

Xcode Version 6.1.1 (6A2008a)

An enum defined in MyEnum.swift

6条回答
  •  囚心锁ツ
    2021-01-30 13:00

    Add this to your code:

    extension MyEnum {
        init?(rawValue: Int) {
            switch rawValue {
            case 0: self = .Zero
            case 1: self = .One
            case 2: self = .Two
            default: return nil
            }
        }
    }
    

提交回复
热议问题