提问人:atoss 提问时间:4/13/2023 更新时间:4/14/2023 访问量:159
当数据是字符串数组时,如何用unarchivedObject替换NSKeyedUnarchiver.unarchiveTopLevelObjectWithData?
How to replace NSKeyedUnarchiver.unarchiveTopLevelObjectWithData with unarchivedObject when data is an array of Strings?
问:
我的项目最近在 Xcode 中显示此错误:
“unarchiveTopLevelObjectWithData”在 macOS 10.14 中已弃用:请改用 unarchivedObject(ofClass:from:)
我发现这篇文章说明了我的确切问题:
当数据是字典时,如何将NSKeyedUnarchiver.unarchiveTopLevelObjectWithData替换为unarchivedObject
除了我的数据是一个字符串数组,而不是字典。所以,因为我不能对那篇帖子发表评论,所以我把这个问题作为一个新问题来问。
如何将以下解决方案应用于 [String]?
let dictionary = try? NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSDictionary.self, NSArray.self], from: data) as? NSDictionary
答:
0赞
atoss
4/14/2023
#1
正确答案是:
let array = try? NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSArray.self, NSString.self], from: data) as? [String]
评论
'NSArray' is not convertible to '[String]'