Ios 索引超出范围数据零swift 3
我有来自Json的数据,并使用alamofireImage将其填充到UIImage中。我从Json中得到的其中一个元素是一个字符串,它有一个指向图像的URL。如果某个特定Json字符串为null或空白,则会出现以下错误: 致命错误:索引超出范围Ios 索引超出范围数据零swift 3,ios,swift,swift3,alamofireimage,Ios,Swift,Swift3,Alamofireimage,我有来自Json的数据,并使用alamofireImage将其填充到UIImage中。我从Json中得到的其中一个元素是一个字符串,它有一个指向图像的URL。如果某个特定Json字符串为null或空白,则会出现以下错误: 致命错误:索引超出范围 请不要使用不带检查的操作员。如果让构造,我真的建议使用 一些伪代码(我不知道您的类型): EventEngine.getEventGalery(invitedID,limit:“2”){(结果,错误)在 DispatchQueue.main.async(
请不要使用
代码>不带检查的操作员。如果让
构造,我真的建议使用
一些伪代码(我不知道您的类型):
EventEngine.getEventGalery(invitedID,limit:“2”){(结果,错误)在
DispatchQueue.main.async(在中执行:{()->Void
如果let response=结果为?EventGaleryResponse{
self.galery=response.data!
让jsonDict=self.galery
如果让dict=jsonDict{
setPhotoFromDict(dict,0,self.imagearry1)
setPhotoFromDict(dict,1,self.imagearry2)
setPhotoFromDict(dict,2,self.imagearry3)
}否则{
打印(“无法反序列化\(字符串(描述:jsonDict)”)
}
}
})
}
私有函数setPhotoFromDict(dict、Int索引、ImageArry){
如果让photo=dict[index].photo作为?字符串,则让url=url(字符串:photo){
imageArry.af_setImage(带url:url)
}
}
最初的错误来自这一行print(“jsonDict\(jsonDict![0].photo!)”
,我想,因为您未经检查就访问对象请不要在未经检查的情况下使用!
操作符。如果让构造,我真的建议使用
一些伪代码(我不知道您的类型):
EventEngine.getEventGalery(invitedID,limit:“2”){(结果,错误)在
DispatchQueue.main.async(在中执行:{()->Void
如果let response=结果为?EventGaleryResponse{
self.galery=response.data!
让jsonDict=self.galery
如果让dict=jsonDict{
setPhotoFromDict(dict,0,self.imagearry1)
setPhotoFromDict(dict,1,self.imagearry2)
setPhotoFromDict(dict,2,self.imagearry3)
}否则{
打印(“无法反序列化\(字符串(描述:jsonDict)”)
}
}
})
}
私有函数setPhotoFromDict(dict、Int索引、ImageArry){
如果让photo=dict[index].photo作为?字符串,则让url=url(字符串:photo){
imageArry.af_setImage(带url:url)
}
}
最初的错误来自这一行print(“jsonDict\(jsonDict![0].photo!)”
,我猜,因为您访问对象时没有检查它是否为空
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!)!)
}
}
}
})
}
我想,您没有检查它是否为空
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!)!)
}
}
}
})
}
您使用“jsonDict[0]”获得了jsonDict的第一个元素,但您没有检查它是否为空。jsonDict可以不为null,也可以为空。jsonDict![0]
命名一个变量Dict,实际上它似乎是一个数组,这太令人恼火了。既然它是一个数组,jsonDict![2]
可能会导致崩溃,因为您没有检查它是否至少有3个元素…(其他元素也是如此)。thaks for Answare。如何检查它是否为空?您正在使用“jsonDict[0]”获取jsonDict的第一个元素,但您没有检查它是否为空。jsonDict可以不为null,也可以为空。jsonDict![0]
当一个var Dict看起来实际上是一个数组时,命名它太令人恼火了。而且因为它是一个数组,jsonDict![2]
可能会导致崩溃,因为你没有检查它是否至少有3个元素…(其他元素也是如此).thaks代表Answare.我如何检查它是否为空?嗨,安东,你所说的DictType和ImageArrayType是什么意思?分别是jsonDict和ImageArrayType的类型。嗨,安东,你所说的DictType和ImageArrayType是什么意思?分别是jsonDict和ImageArrayType的类型。
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!)!)
}
}
}
})
}