使用“!”不推荐使用,将在以后的版本中删除-swift 4.2

时间:2018-09-25 05:44:08

标签: ios optional compiler-warnings sdwebimage swift4.2

在Swift 4.2中使用SDWebimage在单元格中设置图像时,编译器会引发以下警告。

快速编译器警告:

  

使用'!'这里已弃用,并将在以后的版本中删除

let url = NSURL(string: (str_url) as String)

cell.img!.sd_setImage(with: url as URL!, completed: block_image) //--- WARNING ON THIS LINE AT URL!

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

使用此代码:cell. img!.sd_setImage(with: url! as URL, completed: block_image)

建议:使用URL代替NSURL

            let url = URL(string: "" ) //use url String
            cell.img!.sd_setImage(with: url, completed: block_image)

答案 1 :(得分:0)

尝试一下:

if let url = URL(string: str_url) {
    cell.img!.sd_setImage(with: url, completed: block_image)
}