我有字典数组,我试图将其存储在 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/