【问题标题】:my url does not return nil but empty string in swift我的 url 不返回 nil,而是在 swift 中返回空字符串
【发布时间】:2019-02-12 00:47:06
【问题描述】:

下面是我需要获取图像的基于 url 的代码。 但是 url 不是 nil 而是双引号 "" 或空字符串。 因此,我如何处理在 swift 中检查双引号。

DispatchQueue.global(qos: .background).async { [weak self] () -> Void in

    if let url = NSURL(string: arr[indexPath.row].imgl){

             if let data = NSData(contentsOf: url as URL) {

                  DispatchQueue.main.async {
                     cell.img.image = UIImage(data: data as Data)!
                }
              }
          }
   }

如果 url 是 "" 那么它应该转到 else 部分。

【问题讨论】:

  • 你能告诉我们你的json吗?
  • @pkc456 - url 是存储在数组中的字符串 - arr ( arr[indexPath.row].imgl )
  • 共享你的数组保存的字典
  • 你的问题在arr,请给arr
  • 无关,但[weak self] 毫无意义。 DispatchQueue 闭包不会导致保留周期。并且不要在 Swift 中使用 NSURLNSData,使用原生的 URLData。并且从不从具有同步 Data(contentsOf 的远程 URL 加载数据,即使在后台线程中也不行。

标签: ios swift url


【解决方案1】:

转换这个:

NSURL(string: arr[indexPath.row].imgl)

到这里:

URL(string: arr[indexPath.row].imgl)

.

如何在 swift 中处理双引号检查?

if myString.isEmpty {
   print("its empty. Means it is just two double quotes without anything between")
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-28
    • 2018-04-20
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 2020-10-15
    • 2019-09-15
    相关资源
    最近更新 更多