Art*_*kil 16 swift3 ios10 xcode8
我已将我的项目迁移到Swift 3并且NSKeyedArchiver无法正常工作.我在尝试解码这样的对象时实际上有一个运行时错误:
let startDayTime = aDecoder.decodeObject(forKey: Key.startDayTime) as! Int
Run Code Online (Sandbox Code Playgroud)
它在Xcode 7.3中的Swift 2.2中完美运行.还有其他人遇到过这样的麻烦吗?
PS我在模拟器和设备上都有这个错误.
更新:我通过使用decodeInteger(forKey key: String)而不是来解决这个问题decodeObject(forKey key: String).由于某种原因,AnyObject不会在Swift 3中转换为Integer,尽管它在Swift 2.2中也是如此
Jam*_*ang 16
看来,当与存档一个NSData的BLOB这只是发生在斯威夫特2斯威夫特3更新边界NSKeyedArchiver在斯威夫特2上开设有NSKeyedUnarchiver斯威夫特3.我的猜测是,在斯威夫特2,Bool和Int被编码为NSNumber,但在斯威夫特3,它们被编码为原始Bool和Int类型.我相信以下测试支持这种说法:
这适用于Swift 3以解压缩BoolSwift 2中的编码,但nil如果Bool在Swift 3中编码则返回:
let visible = aDecoder.decodeObject(forKey: "visible") as? Bool
Run Code Online (Sandbox Code Playgroud)
这在Swift 3中可以解压缩Swift 3中的Bool编码,但如果Bool是在Swift 2中编码的话会崩溃:
let visible = aDecoder.decodeBool(forKey: "visible")
Run Code Online (Sandbox Code Playgroud)
我的解决方案是:
let visible = aDecoder.decodeObject(forKey: "visible") as? Bool ?? aDecoder.decodeBool(forKey: "visible")
Run Code Online (Sandbox Code Playgroud)
更新:我通过使用decodeInteger(forKey key:String)而不是decodeObject(forKey key:String)解决了这个问题.由于某种原因,AnyObject不会在Swift 3中转换为Integer,尽管它在Swift 2.2中也是如此
| 归档时间: |
|
| 查看次数: |
5580 次 |
| 最近记录: |