【问题标题】:Using '!' here is deprecated and will be removed in a future release - swift 4.2使用 '!'这里已弃用,将在未来的版本中删除 - swift 4.2
【发布时间】:2019-02-28 16:07:08
【问题描述】:

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

Swift 编译器警告:

使用“!”这里已弃用,将在未来的版本中删除

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!

有什么建议吗?

【问题讨论】:

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


    【解决方案1】:

    使用此代码: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)
    

    【讨论】:

    • 请添加Using '!' here is deprecated and will be removed in a future release相关文件
    • @Anbu.karthik 我找到了这份文件,请兄弟审阅:swift.org/blog/iuogithub.com/apple/swift/blob/master/CHANGELOG.md#swift-42 在本文档 Swift 4.2 Section > SE-0054 中。
    • @imgroot 如果您认为答案正确,请批准答案,我会向您解释。试试这个文档,你会得到一个更好的主意:swift.org/blog/iuo
    • 如果您将 x 写为 T!,编译器将首先尝试将其类型检查为 x as T?。仅当失败时,编译器才会尝试将其类型检查为 (x as T?)!,强制可选。
    【解决方案2】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      • 2021-04-12
      • 1970-01-01
      • 2023-04-04
      相关资源
      最近更新 更多