iOS 16.4中如何用unarchivedObject替换旧API解归档字典?
iOS 16.4中NSKeyedUnarchiver弃用API的替换问题
iOS 16.4已弃用unarchiveTopLevelObjectWithData(_:),需替换为unarchivedObject(ofClass:from:)。
当对[String: Any]类型的Swift字典归档后,使用新APINSKeyedUnarchiver.unarchivedObject(ofClass: [String: Any].self, from: data)会出现编译错误,错误提示如下:
Static method 'unarchivedObject(ofClass:from:)' requires that '[String : Any]' conform to 'NSCoding'
相关代码示例:
// 从字典生成归档数据 let dictionary: [String: Any] = ["Text": "Hello", "Number": 1, "Array": ["Hello", "World"]] let data = try! NSKeyedArchiver.archivedData(withRootObject: dictionary, requiringSecureCoding: true) // 尝试从数据还原字典 let dictionary = try? NSKeyedUnarchiver.unarchivedObject(ofClass: [String: Any].self, from: data) // 此代码无法正常编译
内容的提问来源于stack exchange,提问作者Jordan H
相关产品推荐
相关产品推荐

