gpt4 book ai didi

ios - 从 '[Any]?' 转换为无关类型 '[String : String?]' 总是失败问题

转载 作者:搜寻专家 更新时间:2023-11-01 06:00:37 25 4
gpt4 key购买 nike

我使用此代码通过 UserDefaults 存储应用设置:

var appSettings: [String: String?] {

set { // On attempt to set new value for the dictionary
UserDefaults.standard.set(newValue, forKey: "AppSettings")
UserDefaults.standard.synchronize()
}
get { // On attempt to get something from dictionary
if let settings = UserDefaults.standard.array(forKey: "AppSettings") as? [String: String?] {
return settings
} else {
return [:]
}
}}

但是行 if let settings = UserDefaults.standard.array(forKey: "AppSettings") as? [String: String?] 导致警告:

Cast from '[Any]?' to unrelated type '[String : String?]' always fails

关于如何在 UerDefaults 中保存这个字典有什么想法吗?

最佳答案

替换这个

UserDefaults.standard.array(forKey: "AppSettings") as? [String: String?]

UserDefaults.standard.dictionary(forKey: "AppSettings") as? [String: String?]

当你存储一个字典时,所以不要使用 .array(forKey 返回 [Any]? 肯定不能转换为 [字符串:字符串?]


同样来自 Docs

同步()

Waits for any pending asynchronous updates to the defaults database and returns; this method is unnecessary and shouldn't be used.

所以评论

UserDefaults.standard.synchronize()

关于ios - 从 '[Any]?' 转换为无关类型 '[String : String?]' 总是失败问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52432976/

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