枚举的谓词 属性 - swift

Predicate on enum property - swift

我有一个 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.' 而崩溃,但部分对象具有 type 属性,这是字符串枚举。

请帮我解决这个问题

使用“%@”代替“%d”。

let predicate = NSPredicate(格式: "SELF.type == %@", SectionType.Name.rawValue)

错误消息说明了一切。没有从枚举类型到 Objective-C 类型的映射,这意味着 属性 不是 "key value coding-compliant." 您需要使用更简单的 enum : Int 类型来映射自动。