Using '!' here is deprecated and will be r

2020-02-06 16:49发布

问题:

Compiler throwing following warning when setting image in a cell using SDWebimage in Swift 4.2.

Swift Compiler warning :

Using '!' here is deprecated and will be removed in a future release

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!

Any Suggestions ?

回答1:

Use this code : cell. img!.sd_setImage(with: url! as URL, completed: block_image)

Suggestion: use URL instead of NSURL

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


回答2:

Try this:

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