加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 枚举值和谓词

发布时间:2020-12-14 04:33:29 所属栏目:百科 来源:网络整理
导读:有没有办法在不调用rawValue的情况下在NSPredicate中使用枚举值? 例如: enum MyEnum: Int32 { case A,B,C}var predicate: NSPredicate = NSPredicate(format: "prop_status == %i",argumentArray: [MyEnum.A]) 我正在考虑扩展NSPredicate以便它可以处理MyE
有没有办法在不调用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)])

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读