Ios Swift可选(3)在展开时获取零
我是新来的,我有一个非常奇怪的问题。 该应用程序通过JSON从外部PHP文件加载数据,将其展开并将结果集保存在数组中。奇怪的是,当类别为1时,一切正常,但当类别为2时,可选值(例如可选值(5))变为零 代码如下:Ios Swift可选(3)在展开时获取零,ios,swift,swift2,Ios,Swift,Swift2,我是新来的,我有一个非常奇怪的问题。 该应用程序通过JSON从外部PHP文件加载数据,将其展开并将结果集保存在数组中。奇怪的是,当类别为1时,一切正常,但当类别为2时,可选值(例如可选值(5))变为零 代码如下: var jsonElement: NSDictionary = NSDictionary() let users: NSMutableArray = NSMutableArray() for(var i = 0; i < jsonResult.count; i+
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);
)如何正确使用可选的装订和铸造
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
}
让d:[字符串:任意]=[“a”:“a”,“b”:“b”]
设v=d[“a”]
打印(v,v.dynamicType)//可选(“A”)可选
如果设v=d[“a”]as?串{
print(v,v.dynamicType)//字符串
}
这是我想到的。应该足够近,以向您展示如何使用可选绑定
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)
}
}
}
}
您在一个
if let
表达式中列出了所有可能的字段,只有当所有字段都是而不是nil
时才是真的
在您的情况下,“let Question=jsonElement[“Question”]!as?String”将因“Category=2”而失败。因此,
if let
块不会被执行。当我这样做时,数组中的每个值都会得到nil,我不会得到它。我添加它。我希望这有帮助。告诉我们你的jsonResult是什么样子。我把它作为第二个答案添加。仅供参考:快速创建数组的方法:让用户:NSMutableArray=NSMutableArray()!我怎么会错过这个。
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)
}
}
}
}