【问题标题】:Index out of range data nil swift 3索引超出范围数据 nil swift 3
【发布时间】:2018-01-19 08:43:19
【问题描述】:

我有来自 Json 的数据并使用 alamofireImage 将其填充到 UIImage 中。我从 Json 得到的其中一个元素是一个具有图像 URL 的字符串。如果特定的 Json 字符串为空或空白,我会收到以下错误:

致命错误:索引超出范围

EventEngine.getEventGalery(invitedID, limit: "2") { (result, error) in
    DispatchQueue.main.async(execute: { () -> Void in

        if let response = result as? EventGaleryResponse{

            self.galery = response.data!

            let jsonDict = self.galery
            print("jsonDict \(jsonDict![0].photo!)")
            if jsonDict![0].photo != nil {
                self.imagearry1.af_setImage(withURL: URL(string: jsonDict![0].photo!)!)
            }
            if jsonDict![1].photo != nil {
                self.imagearry2.af_setImage(withURL: URL(string:jsonDict![1].photo!)!)
            }
            if jsonDict![2].photo != nil {
                self.imagearry3.af_setImage(withURL: URL(string: jsonDict![2].photo!)!)
            }
        }
    })
}

【问题讨论】:

  • 您正在获取 jsonDict 的第一个元素,使用“jsonDict[0]”,但您没有检查它是否为空。 jsonDict 可以不为空,也可以为空。
  • jsonDict![0] 命名一个 var Dict 实在是太烦人了,而实际上它似乎是一个数组。由于它是一个数组,jsonDict![2] 可能会导致崩溃,因为您没有检查它是否至少有 3 个元素......(其他元素依此类推)。
  • 感谢您的回答。如何检查它是否为空?

标签: ios swift swift3 alamofireimage


【解决方案1】:

请不要在没有检查的情况下使用! 运算符。我真的建议改用if let 构造。

一些伪代码(我不知道你的类型):

EventEngine.getEventGalery(invitedID, limit: "2") { (result, error) in
    DispatchQueue.main.async(execute: { () -> Void in

        if let response = result as? EventGaleryResponse{

            self.galery = response.data!

            let jsonDict = self.galery

            if let dict = jsonDict {
                setPhotoFromDict(dict, 0, self.imagearry1)
                setPhotoFromDict(dict, 1, self.imagearry2)
                setPhotoFromDict(dict, 2, self.imagearry3)
            } else {
                print("cannot deserialise \(String(describing: jsonDict)")
            }
        }
    })
}

private func setPhotoFromDict(<#DictType#> dict, Int index, <#ImageArrayType#> imageArary) {
    if let photo = dict[index].photo as? String, let url = URL(string: photo) {
        imageArary.af_setImage(withURL: url)
    }
}

我猜,最初的错误来自print("jsonDict \(jsonDict![0].photo!)")这一行,因为你访问对象时没有检查

【讨论】:

  • 嗨安东,你所说的 DictType 和 ImageArrayType 是什么意思?
  • jsonDict 和 imagearry 的类型。
【解决方案2】:

我想,你没有检查它是否为空

EventEngine.getEventGalery(invitedID, limit: "2") { (result, error) in
            DispatchQueue.main.async(execute: { () -> Void in

                if let response = result as? EventGaleryResponse{

                    self.galery = response.data!

                    let jsonDict = self.galery

                   if jsonDict != nil {

                    if jsonDict.count > 0 && jsonDict![0].photo != nil {
                        self.imagearry1.af_setImage(withURL: URL(string: jsonDict![0].photo!)!)
                    }
                    if jsonDict.count > 1 && jsonDict![1].photo != nil {
                        self.imagearry2.af_setImage(withURL: URL(string:jsonDict![1].photo!)!)
                    }
                    if jsonDict.count > 2 && jsonDict![2].photo != nil {
                        self.imagearry3.af_setImage(withURL: URL(string: jsonDict![2].photo!)!)
                    }

                  }
                }
            })
        }

【讨论】:

    猜你喜欢
    • 2017-05-14
    • 1970-01-01
    • 2018-05-07
    • 2016-11-10
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    • 2017-12-07
    相关资源
    最近更新 更多