美文网首页
swift enum 关联值

swift enum 关联值

作者: LimChihi | 来源:发表于2016-12-14 21:26 被阅读104次

如果在swift中为枚举设置了关联值.

enum Country {
    case China(city: String)
    case Japan(city: String)
    case Singapore
}

那么直接判断相等是错误的

let sig = Country.Singapore
if sig == Country.Singapore {
    //...
}

要这样判断相等

let sig = Country.Singapore
if case Country.Singapore = sig {
    //...
}
let gz = Country.Cina(city: "GuangZhou")
if case Country.Cina(city: "GuangZhou") = gz {
    //...
}

相关文章

网友评论

      本文标题:swift enum 关联值

      本文链接:https://www.haomeiwen.com/subject/ickwmttx.html