【问题标题】:Swift NSURL error while creating [duplicate]创建时出现Swift NSURL错误[重复]
【发布时间】:2016-06-01 05:21:48
【问题描述】:

这是我的代码。错误:fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)我不知道哪里出了问题

res = "https://www.example.com/range.php?arr1=48.15&arr2=48.15&API_KEY=>code"
    let url = NSURL(string: res)! // error line
    print("url craeted" + res)
    return url

【问题讨论】:

标签: ios iphone swift


【解决方案1】:

组合 => 导致指令失败,让我们不要将字符串解释为字符串。 顺便说一句,我假设 res 已在您的代码中的其他地方定义,并且您想稍后将“url created”更改为“url created”。

【讨论】:

    【解决方案2】:

    您可以使用扩展来编码您的网址

    extension String {
        func urlEncodedString() -> String {
            let urlEncodedString = self.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
            return urlEncodedString ?? self
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多