gpt4 book ai didi

swift - 将 NSDictionary 设置为 UserDefaults 时出现异常

转载 作者:搜寻专家 更新时间:2023-11-01 05:56:05 24 4
gpt4 key购买 nike

如何修复异常:

[User Defaults] Attempt to set a non-property-list object {
4 = 5;
} as an NSUserDefaults/CFPreferences value for key MyKey

通过执行以下代码(Swift 3)抛出

let ns = NSDictionary(dictionary: [4: 5])
UserDefaults.standard.set(ns, forKey: "MyKey")

上面的代码正确保存了 String 类型,但是 NSDictionary 有问题

最佳答案

所有属性列表键都必须是 String

来自documentation

And although NSDictionary and CFDictionary objects allow their keys to be objects of any type, if the keys are not string objects, the collections are not property-list objects.

let ns = ["4": 5]
UserDefaults.standard.set(ns, forKey: "MyKey")

无需使用 NSDictionary,Swift Dictionary 的工作方式相同,并且是隐式桥接的。

关于swift - 将 NSDictionary 设置为 UserDefaults 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44415250/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com