2016-02-19 77 views
-2

這是我的代碼。錯誤:fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)我不知道在哪裏; S問題Swift創建NSURL時出錯

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 
+5

'>'不是URL的有效字符。編碼您的URL:http://stackoverflow.com/q/24551816/2227743,http://stackoverflow.com/a/30149081/2227743等 – Moritz

回答

0

組合=>導致的指令失敗,讓我們的字符串不解釋爲一個字符串。 順便說一句,我假設res已經在你的代碼的其他地方被定義了,你想在以後的版本中將「url created」改成「url created」。

0

您可以使用擴展編碼您的網址

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