2016-08-23 98 views
0

我有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屬性,這是字符串的枚舉。

請幫我解決問題

+1

爲什麼不用'.filter'代替謂詞? – Tj3n

+0

是的,我們可以,但只是好奇地得到它這樣做 – Saif

回答

1

錯誤消息說,這一切。沒有從枚舉類型映射到Objective-C類型,這意味着該屬性不是「符合關鍵值編碼」。您需要使用更簡單的enum : Int類型才能自動映射。

+0

我使用'.filter'而不是'NSPredicate'建議由@ Tj3n – Saif

0

使用「%@」代替「%d」。

令謂詞= NSPredicate(格式爲: 「SELF.type ==%@」,SectionType.Name.rawValue)