"-6ren"> "-{ deptId = 57f1fe3d3ac3aca0059d4d25; deptName = "Eagle Security"; licences = ( -6ren">
gpt4 book ai didi

ios - 在快速存储 NSDictionary 时出错,其中包含一些值,例如 userDefaults 中的 ""

转载 作者:行者123 更新时间:2023-11-28 09:29:34 24 4
gpt4 key购买 nike

{
deptId = 57f1fe3d3ac3aca0059d4d25;
deptName = "Eagle Security";
licences = (
{
"_id" = 57787880f9121e1b0516fb6b;
name = "<null>";
},
{
"_id" = 57787881f9121e1b0516fb6c;
name = "<null>";
},
{
"_id" = 57787883f9121e1b0516fb6d;
name = " Mr.Apachu - SEC";
},
{
"_id" = 57787884f9121e1b0516fb6e;
name = "Mr.John Peter - SEC";
},
{
"_id" = 578b3be488170556a2860a63;
name = "Mr. Ramu - SEC";
})}

我试图将这个 NSDictionary 存储在 userdefaults 中,但它给我错误。有哥们能告诉我怎么把它存到用户默认值里吗?

最佳答案

do {
let jsonData: Data = try JSONSerialization.data(withJSONObject: yourNSDictionary, options: .prettyPrinted)
let strJson:String = String(data: jsonData, encoding: .utf8)!
let strJsonWithNoNull = strJson.replacingOccurrences(of: "<null>", with: "")

let data = strJsonWithNoNull.data(using: .utf8)!

do {
let dictonary = try JSONSerialization.jsonObject(with: data, options: []) as? [String:Any]

if dictonary != nil
{
//you can store it inside user defaults
}
} catch let error as NSError {
print(error)
}


} catch let error as NSError {
print(error)
}

关于ios - 在快速存储 NSDictionary 时出错,其中包含一些值,例如 userDefaults 中的 "<null>",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41329504/

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