【问题标题】:Swift Optional(3) gets nil when unwrapping展开时 Swift Optional(3) 变为零
【发布时间】: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) 不再工作了

标签: ios swift swift2


【解决方案1】:

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

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

【讨论】:

  • 谢谢!我怎么能错过这个。
【解决方案2】:

如何正确使用可选绑定和强制转换

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
}

【讨论】:

  • 当我这样做时,数组中的每个值都为零,我不明白。
  • 我加了。我希望这会有所帮助。
【解决方案3】:

这是我想出来的。应该足够接近以向您展示如何使用可选绑定。

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)
                    }
                }
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    相关资源
    最近更新 更多