InitRequiringSecureCodeding-swift
我使用一个函数将数据保存到文件中:InitRequiringSecureCodeding-swift,swift,Swift,我使用一个函数将数据保存到文件中: let data = NSMutableData() let archiver = NSKeyedArchiver(forWritingWith: data) archiver.encode(Elements, forKey: "Elements") archiver.finishEncoding() data.write(toFile: dataFilePath(), at
let data = NSMutableData()
let archiver = NSKeyedArchiver(forWritingWith: data)
archiver.encode(Elements, forKey: "Elements")
archiver.finishEncoding()
data.write(toFile: dataFilePath(), atomically: true)
更新到swift 5后,将出现如下警告:
'init(forWritingWith:)' was deprecated in iOS 12.0: Use -initRequiringSecureCoding: instead
我尝试使用:
let archiver = NSKeyedArchiver(requiringSecureCoding: true)
let data = archiver.encodedData
当我试图保存信息时,它给出了一个错误:
'This decoder will only decode classes that adopt NSSecureCoding. Class 'Elements' does not adopt it.'
有什么建议吗?更换
let archiver = NSKeyedArchiver(forWritingWith: data)
与
想一想
Codable
而不是旧的ObjcNSCoding
我以前试过这个,但这还不够,因为我需要在数据文件上写。这里有人可以帮忙吗?
let archiver = NSKeyedArchiver(requiringSecureCoding: true)