【发布时间】:2016-10-10 11:03:34
【问题描述】:
我有一个enum:
public enum PersonType:String {
case Cool = "cool"
case Nice = "rude"
case SoLazy = "so-lazy"
public var description: String {
switch self {
case .Cool:
return "Cool person"
case .Nice:
return "Nice person"
case .SoLazy:
return "its so lazy person"
}
}
public var typeImage: String {
switch self {
case .Cool:
return "cool.png"
case .Nice:
return "img_nice.png"
case .Solazy:
return "lazy.png"
}
}
}
问题我不知道所有的 person 类型键,所以我需要处理 person 类型的默认情况并给它描述将是它的键,如“so-lazy”和默认图像。
假设我从网络服务中得到这个结果:
[
{
name: "john",
key: "cool"
},
{
name: "paul",
key: "funny"
}
]
我需要一个默认案例来处理关键“有趣”
这是我在解析和创建人员对象时初始化枚举的方式:
if let personType = PersonType(rawValue:personTypeKey ?? "") {
self.personType = personType
}
我想要一个else 或更好的方法来处理我的枚举中未知键的情况,并将键作为描述和默认图像提供给它们。
【问题讨论】:
标签: ios swift data-structures enums swift2