enum Seat: String{
case middle
case aisle
case window
case undefined
}
let s : Seat = Seat(rawValue: Seat.middle)
这样做给了我
error: invalid initializer call with same type 'Seat' as parameter
这似乎是一个简单的错误。我在网上搜索但没有找到任何东西。
最佳答案
错误很明显。
rawValue
参数需要一个 String
。但是你给它的是枚举类型本身。
你应该这样做:
let s: Seat = .middle
let s = Seat(rawValue: "middle") ?? .undefined
let s = Seat(rawValue: "middle")! // Perhaps this is better, per Alexander's comment
或仅用于演示目的:
let s = Seat(rawValue: Seat.middle.rawValue) ?? .undefined
我通过将 s
属性的类型从 String
更改为 Seat
造成了这个错误。但是由于更改是在与定义枚举的文件不同的文件中进行的……我感到困惑。
关于swift - 具有相同类型 'XXXX' 作为参数的无效初始化程序调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50241435/