【发布时间】:2016-03-12 06:06:38
【问题描述】:
我是新来的,遇到了一个非常奇怪的问题。 应用程序通过 JSON 从外部 PHP 文件加载数据,解包并将结果集保存在数组中。奇怪的是,当 Category 为 1 时一切正常,但当它为 2 时, Optional 值例如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);
以下是控制台的打印值:
数据下载 可选(1) 1 可选(1) 1 可选(1) 1 可选(2) 可选(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;
}
)
【问题讨论】:
-
向我们展示您的 jsonResult 的样子。
-
我将其添加为第二个答案。
-
仅供参考:Swift 处理数组的方式:
let users: NSMutableArray = NSMutableArray()可以更改为:let users = [AnyObject]() -
当我改变它时 users.addObject(user) 不再工作了