最新消息:20190529 VPS服务器已从腾讯云香港换为Vultr新加坡,主题仍用朋友推荐的大前端D8

[已解决]swift中枚举值decodeObjectForKey失败

Swift crifan 787浏览 0评论

折腾:

[已解决]swift中enum枚举变量没法被aCoder.encodeObject

期间,用代码:

self.personType = aDecoder.decodeObjectForKey(codingKey_personType) as! PersonType

出错:

EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP, subcode = 0x0)

相关部分代码为:

class PersonItem: ContactItem {
    var personType:PersonType
    var phoneNumber:String
    var email:String
    var position:String
    override init() {
        personType = PersonType.Invalid
        phoneNumber = ""
        email = ""
        position = ""
       
        super.init()
        commonInit()
    }
    // MARK: NSCoding
    private let codingKey_personType = "PersonItem_personType"
    private let codingKey_phoneNumber = "PersonItem_phoneNumber"
    private let codingKey_email = "PersonItem_email"
    private let codingKey_position = "PersonItem_position"
   
    required init?(coder aDecoder: NSCoder){
        self.personType = aDecoder.decodeObjectForKey(codingKey_personType) as! PersonType
        self.phoneNumber = aDecoder.decodeObjectForKey(codingKey_phoneNumber) as! String
        self.email = aDecoder.decodeObjectForKey(codingKey_email) as! String
        self.position = aDecoder.decodeObjectForKey(codingKey_position) as! String
       
        super.init(coder: aDecoder)
       
        commonInit()
    }
   
    override func encodeWithCoder(aCoder: NSCoder){
        super.encodeWithCoder(aCoder)
        aCoder.encodeObject(personType.rawValue, forKey: codingKey_personType)
        aCoder.encodeObject(phoneNumber, forKey: codingKey_phoneNumber)
        aCoder.encodeObject(email, forKey: codingKey_email)
        aCoder.encodeObject(position, forKey: codingKey_position)
    }
   
    func commonInit(){
        self.type = ContactType.Person
    }
}

搜:

swift enum decodeObjectForKey fail

ios – EXC_BAD_INSTRUCTION in NSCoding of Swift – Stack Overflow

swift enum decodeObjectForKey EXC_BAD

archive array of optional structs with NSCoding in Swift? – Stack Overflow

EXC_BAD_INSTRUCTION in NSCoding of Swift

EXC_BAD_INSTRUCTION when unarchiving Swift Dictionary – btday.com

swift unarchive enum  EXC_BAD

ios unarchive enum  EXC_BAD

搜:

swift enum archive

Swift enum and NSCoding – Stack Overflow

How do I encode enum using NSCoder in swift? – Stack Overflow

【总结】

后来改为:

//        self.personType = aDecoder.decodeObjectForKey(codingKey_personType) as! PersonType
        let rawPersonTypeStr = aDecoder.decodeObjectForKey(codingKey_personType) as! String //"Colleague"
        self.personType = PersonType(rawValue: rawPersonTypeStr)! //JianDao.PersonType.Colleague

即可。

完整代码:

enum PersonType:String {
    case Invalid
    case Friend
    case Colleague
    case Bot
    case Myself
}
class PersonItem: ContactItem {
    var personType:PersonType
    override init() {
        personType = PersonType.Invalid 
        super.init()
    }
    // MARK: NSCoding
    private let codingKey_personType = "PersonItem_personType"
    required init?(coder aDecoder: NSCoder){
//        self.personType = aDecoder.decodeObjectForKey(codingKey_personType) as! PersonType
//        let rawPersonTypeStr = aDecoder.decodeObjectForKey(codingKey_personType) as! String //"Colleague"
//        self.personType = PersonType(rawValue: rawPersonTypeStr)! //JianDao.PersonType.Colleague
        self.personType = PersonType(rawValue: aDecoder.decodeObjectForKey(codingKey_personType) as! String)!
        super.init(coder: aDecoder)
    }
   
    override func encodeWithCoder(aCoder: NSCoder){
        aCoder.encodeObject(personType.rawValue, forKey: codingKey_personType)
       
        super.encodeWithCoder(aCoder)
    }
}

供参考。

转载请注明:在路上 » [已解决]swift中枚举值decodeObjectForKey失败

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
62 queries in 0.137 seconds, using 18.95MB memory