微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift – 枚举值和谓词

有没有办法在不调用rawValue的情况下在nspredicate中使用枚举值

例如:

enum MyEnum: Int32 {
    case A,B,C
}

var predicate: nspredicate = nspredicate(format: "prop_status == %i",argumentArray: [MyEnum.A])

我正在考虑扩展nspredicate以便它可以处理MyEnum但不知道如何做到这一点.

解决方法

一种方法是使用customstringconvertible,当您遵循此协议时,您需要实现返回String类型的描述方法.然后你可以使用String.init方法获取字符串值.

有点像这样,

enum MyEnum: Int32,customstringconvertible {
    case A,C

    var description: String {
        return "\(rawValue)"
    }
}

var predicate: nspredicate = nspredicate(format: "prop_status == %@",argumentArray: [String(MyEnum.A)])

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐