我是新来的,我有一个非常奇怪的问题。 该应用程序通过JSON从外部PHP文件加载数据,解包并将结果集保存在数组中。奇怪的是,当类别为1时,一切正常,但是当它是2的可选值时,例如可选(5)获得nil。
以下是代码:
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)( &#34; FID:1,类别:1,A1:Ja,A2:Nein,UID:1,年份:零,性别:零,点数:零,CID:1,评论:test&#34;, &#34; FID:2,类别:1,A1:Ja,A2:Nein,UID:1,年份:零,性别:零,点数:零,CID:2,评论:test&#34;, &#34; FID:3,类别:1,A1:Ja,A2:Nein,UID:1,年份:零,性别:零,点数:零,CID:3,评论:test&#34;, &#34; FID:零,类别:零,A1:零,A2:零,UID:零,年份:零,性别:零,点数:零,CID:零,评论:零&#34; )
正如您所看到的,数组中的最后一个值都是零,但是当我显示整个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;
}
)
答案 0 :(得分:3)
您在一个if let
表达式中列出了所有可能的字段,仅当所有字段都为not nil
时才会出现。
在你的情况下&#34;让问题= jsonElement [&#34;问题&#34;]!如?字符串&#34;将失败的&#34;类别= 2&#34;。因此if let
块不会被执行。
答案 1 :(得分:0)
如何正确使用可选的绑定和转换
let d:[String:Any] = ["a":"A","b":"B"]
let v = d["a"]
print(v, v.dynamicType) // Optional("A") Optional<protocol<>>
if let v = d["a"] as? String {
print(v, v.dynamicType) // A String
}
答案 2 :(得分:0)
这是我想出来的。应足够接近,向您展示如何使用可选绑定。
if let items = jsonResult as? [AnyObject] {
items.forEach {
item in
if let parsedItem = item as? [String: AnyObject] {
if let question = parsedItem["question"] as? String,
// continue chaining {
var user = User() // initialize user
users.addObject(user)
}
}
}
}