简体   繁体   English

枚举属性谓词-Swift

[英]Predicate on enum property - swift

i have an array of Section objects 我有一个Section对象数组

class Section: NSObject {

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

i need to apply predicate on type property 我需要对type属性应用谓词

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

}

App is crashing with Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Mobile.Section 0x7fb120d33c50> valueForUndefinedKey:]: this class is not key value coding-compliant for the key type.' Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Mobile.Section 0x7fb120d33c50> valueForUndefinedKey:]: this class is not key value coding-compliant for the key type.'Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Mobile.Section 0x7fb120d33c50> valueForUndefinedKey:]: this class is not key value coding-compliant for the key type.'崩溃Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Mobile.Section 0x7fb120d33c50> valueForUndefinedKey:]: this class is not key value coding-compliant for the key type.' , but the section object has type property, which is enum of string. ,但section对象具有type属性,它是字符串的枚举。

Please help me fixing the issue 请帮助我解决问题

The error message says it all. 错误消息说明了一切。 There's no mapping from your enum type to an Objective-C type, which means the property isn't "key value coding-compliant." 没有从您的枚举类型到Objective-C类型的映射,这意味着该属性不符合“键值编码标准”。 You'd need to use a simpler enum : Int type for this to map automatically. 您需要使用一个更简单的enum : Int类型,以便自动映射。

use "%@" instead "%d". 使用“%@”而不是“%d”。

let predicate = NSPredicate(format: "SELF.type == %@", SectionType.Name.rawValue) 让谓词= NSPredicate(格式:“ SELF.type ==%@”,SectionType.Name.rawValue)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM