我有一组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
属性,它是字符串的枚举。
请帮我解决问题
答案 0 :(得分:1)
错误消息说明了一切。从您的枚举类型到Objective-C类型没有映射,这意味着该属性不符合键值编码。&#34;您需要使用更简单的enum : Int
类型来自动映射。
答案 1 :(得分:0)
使用&#34;%@&#34;相反&#34;%d&#34;。
让谓词= NSPredicate(格式:&#34; SELF.type ==%@&#34;,SectionType.Name.rawValue)