致命错误:索引超出范围:Swift

时间:2017-08-30 14:05:23

标签: ios json swift

我有JSON图片。

let imagestring : String? = (myData as AnyObject).value(forKey: "Post_mid_image") as? String

if imagestring != nil{
    let imageTrueString = "https://www.zdoof.com/" + imagestring!
    self.imageStringArray.append(imageTrueString )
    print(self.imageStringArray)
}

当我试图把它放在桌面视图上时:

let myImage = cell.viewWithTag(30) as! UIImageView
let myImageString : String? = imageStringArray[indexPath.row]

if myImageString != nil{
    let ImageUrl = URL.init(string: myImageString!)

    myImage.kf.setImage(with: ImageUrl)
} 

显示如下错误:

  

致命错误:索引超出范围

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

这样的行数方法

Column1      Column2
Test         Test 12212
Test         Test 6588476
Something    Something 4125
What         What is this 1254

使用如果让来避免强行投射

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return imageStringArray.count
    }

<强>更新: -

if let myImage = cell.viewWithTag(30) as? UIImageView {
    if let myImageString = imageStringArray[indexPath.row] as? String {
        let ImageUrl = URL.init(string: myImageString!)
        myImage.kf.setImage(with: ImageUrl)

    }

}