NSKeyedArchiver does not work in Swift 3 ( XCode 8)

后端 未结 4 530
悲哀的现实
悲哀的现实 2021-02-02 14:41

I have migrated my project to Swift 3 and NSKeyedArchiver does not work. I actually have a runtime error when trying to decode object like this:

let         


        
4条回答
  •  温柔的废话
    2021-02-02 15:26

    It appears that this only happens on the Swift 2 to Swift 3 update boundary when a NSData blob archived with a NSKeyedArchiver in Swift 2 is opened with a NSKeyedUnarchiver in Swift 3. My guess is that on Swift 2, the Bool and Int are encoded as NSNumber, but in Swift 3, they are encoded as raw Bool and Int types. I believe the following test supports this claim:

    This works in Swift 3 to unarchive a Bool encoded in Swift 2, but returns nil if the Bool was encoded in Swift 3:

    let visible = aDecoder.decodeObject(forKey: "visible") as? Bool
    

    This works in Swift 3 to unarchive a Bool encoded in Swift 3, but crashes if the Bool was encoded in Swift 2:

    let visible = aDecoder.decodeBool(forKey: "visible")
    

    My solution is:

    let visible = aDecoder.decodeObject(forKey: "visible") as? Bool ?? aDecoder.decodeBool(forKey: "visible")
    

提交回复
热议问题