关于枚举属性的谓词 - 迅捷

时间:2016-08-23 10:01:58

标签: ios arrays swift enums nspredicate

我有一组Section个对象

class Section: NSObject {

    enum SectionType : String {
        case Name = "NAME"
        case Address = "ADDRESS"
        case Phone = "PHONE"
    }
    var type : SectionType = .Name
} 

我需要在type属性

上应用谓词
let predicate = NSPredicate(format: "SELF.type == %d", SectionType.Name.rawValue)
let filteredArray = (preferenceSections as NSArray).filteredArrayUsingPredicate(predicate) //CRASH 
if filteredArray.count > 0 {

}

应用程序正在与Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Mobile.Section 0x7fb120d33c50> valueForUndefinedKey:]: this class is not key value coding-compliant for the key type.'崩溃,但是section对象具有type属性,它是字符串的枚举。

请帮我解决问题

2 个答案:

答案 0 :(得分:1)

错误消息说明了一切。从您的枚举类型到Objective-C类型没有映射,这意味着该属性不符合键值编码。&#34;您需要使用更简单的enum : Int类型来自动映射。

答案 1 :(得分:0)

使用&#34;%@&#34;相反&#34;%d&#34;。

让谓词= NSPredicate(格式:&#34; SELF.type ==%@&#34;,SectionType.Name.rawValue)