gpt4 book ai didi

ios - Swift Optional(3) 在展开时得到 nil

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

我是新来的,我有一个非常奇怪的问题。该应用程序通过 JSON 从外部 PHP 文件加载数据,解包并将结果集保存在数组中。奇怪的是,当 Category 为 1 时一切正常,但是当它为 2 时 Optional 值例如可选 (5) 变为零。

代码如下:

var jsonElement: NSDictionary = NSDictionary()
let users: NSMutableArray = NSMutableArray()

for(var i = 0; i < jsonResult.count; i++)
{


jsonElement = jsonResult[i] as! NSDictionary
// print(jsonResult)
let user = UserModel();
print((Int32(jsonElement["Category"]! as! String)))

//the following insures none of the JsonElement values are nil through optional binding
if((Int32(jsonElement["Category"]! as! String)) == 1){

if let FID = Int32(jsonElement["FID"]! as! String),let Category = Int32(jsonElement["Category"]! as! String)
,let UID = Int32(jsonElement["UID"]! as! String),let Comment = jsonElement["Comment"]! as? String,let A1 = jsonElement["A1"]! as? String,let A2 = jsonElement["A2"]! as? String,let Question = jsonElement["Question"]! as? String, let CID = Int32(jsonElement["CID"]! as! String){
print(Category)
user.FID = FID;
user.Category = Category;
user.UID = UID
user.Comment = Comment
user.A1 = A1;
user.A2 = A2;
user.Question = Question;
user.CID = CID;
}





users.addObject(user)
//print(user)
}
if((Int32(jsonElement["Category"]! as! String)) == 2){

print(Int32(jsonElement["FID"]! as! String))

if let FID = Int32(jsonElement["FID"]! as! String),let Category = Int32(jsonElement["Category"]! as! String)
,let UID = Int32(jsonElement["UID"]! as! String),let Comment = jsonElement["Comment"]! as? String,let Img1ID = Int32(jsonElement["Img1ID"]! as! String),let Img2ID = Int32(jsonElement["Img2ID"]! as! String),let Question = jsonElement["Question"]! as? String, let CID = Int32(jsonElement["CID"]! as! String){
print(Category)
user.FID = FID;
user.Category = Category;
user.UID = UID
user.Comment = Comment
user.Img1ID = Img1ID;
user.Img2ID = Img2ID;
user.Question = Question;
user.CID = CID;
}

users.addObject(user)


}

}

print(users);

这里是控制台打印的值:

Data downloaded Optional(1) 1 Optional(1) 1 Optional(1) 1 Optional(2) Optional(4) ( "FID: 1, Category: 1, A1: Ja, A2: Nein, UID: 1, Year: nil, Sex: nil, Points: nil, CID: 1, Comment: test", "FID: 2, Category: 1, A1: Ja, A2: Nein, UID: 1, Year: nil, Sex: nil, Points: nil, CID: 2, Comment: test", "FID: 3, Category: 1, A1: Ja, A2: Nein, UID: 1, Year: nil, Sex: nil, Points: nil, CID: 3, Comment: test", "FID: nil, Category: nil, A1: nil, A2: nil, UID: nil, Year: nil, Sex: nil, Points: nil, CID: nil, Comment: nil" )

如您所见,数组中的最后一个值都是 nil,但是当我显示整个 JsonResult 时,存在匹配的值。没有人知道发生了什么事吗?是否有更好的解包值解决方案?

我真的很想听听你们的意见!

这是 jsonResult:

(
{
A1 = Ja;
A2 = Nein;
CID = 1;
Category = 1;
Comment = "test";
FID = 1;
Question = "test";
UID = 1;
},
{
A1 = Ja;
A2 = Nein;
CID = 2;
Category = 1;
Comment = "test";
FID = 2;
Question = "test";
UID = 1;
},
{
A1 = Ja;
A2 = Nein;
CID = 3;
Category = 1;
Comment = "test";
FID = 3;
Question = "test";
UID = 1;
},
{
CID = 4;
Category = 2;
Comment = "test";
FID = 4;
Img1ID = 1;
Img2ID = 2;
UID = 1;
}

)

最佳答案

您在一个 if let 表达式中列出了所有可能的字段,只有当所有字段都为 not nil 时才为真。

在您的情况下,“let Question = jsonElement["Question"]!as?String”将因“Category = 2”而失败。所以 if let block 不会被执行。

关于ios - Swift Optional(3) 在展开时得到 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35949651/

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