You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 23:47:40