gpt4 book ai didi

ios - 如何快速替换字典数组中的值?

转载 作者:行者123 更新时间:2023-12-01 22:21:40 26 4
gpt4 key购买 nike

我有字典数组,我试图将其存储在 NSuserdefault 中,但因为它包含 <null>应用程序崩溃,我们可以更换 <null>和 ””?有什么办法可以解决这个问题吗?

这是我的字典数组:

var array = (
{
ClinicID = "<null>";
"Patient_SurveyID" = 1956;
"Patient_Treatment_PlanID" = "<null>";
PhysicianID = "<null>";
},
{
ClinicID = "<null>";
"Patient_SurveyID" = 1956;
"Patient_Treatment_PlanID" = "<null>";
PhysicianID = "<null>";
},
)

最佳答案

所以你有一个字典数组

let list: [[String:Any]] = []

每个字典的类型都是[String:Any]

这是将 NSNull 值替换到字典中的代码“”

let list: [[String:Any] = []

let updatedDict = list.map { (dict) -> [String:Any] in
let keysWithEmptStringValue = dict.filter { $0.1 is NSNull }.map { $0.0 }
var dict = dict
for key in keysWithEmptStringValue {
dict[key] = ""
}
return dict
}

从 NSMutableArray 到 [[String:Any]]

要将 NSMutableArray 转换为 Swift 通用数组,请尝试此代码

let array = NSMutableArray()
let list: [[String:Any]] = array.flatMap { $0 as? [String:Any] }

关于ios - 如何快速替换字典数组中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38456966/

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