Ios 创建时出现Swift NSURL错误

Ios 创建时出现Swift NSURL错误,ios,iphone,swift,Ios,Iphone,Swift,这是我的密码。错误:致命错误:在展开可选值时意外发现nil (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 您可以使用扩展名对URL进行编

这是我的密码。错误:
致命错误:在展开可选值时意外发现nil
(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

您可以使用扩展名对URL进行编码

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

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

不是URL的有效字符。编码你的网址:,等等