NSURL在解开Optional值时发现nil

时间:2015-03-05 16:35:09

标签: swift nsurl nsurlrequest

任何人都知道为什么我

致命错误:在解包可选值时意外发现nil

当我使用

    let URL = NSURL(string: "https://roads.googleapis.com/v1/snapToRoads?path=-35.27801,149.12958|-35.28032,149.12907")!

3 个答案:

答案 0 :(得分:15)

|字符不是有效的URL字符,因此您必须将其替换为%转义字符。编码整个字符串将自动为您执行

var stringUrl = "https://roads.googleapis.com/v1/snapToRoads?path=-35.27801,149.12958|-35.28032,149.12907"

let URL = NSURL(string: stringUrl.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!)!

答案 1 :(得分:0)

快捷键4

我正在尝试:

https://api.url.com/method?token=abcdfghijklmopqrst&command=>SSSXP10<&otherParam=12345678

> ”和“ << / strong>”字符给了我错误。

对于解决方案:

let objectUrl = URL(string:url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)

答案 2 :(得分:0)

哦,这是一个疯狂的问题。由于 << / strong>和> ,导致出现错误

let username = searchTextFileld.text!
let urlString: String = "https://api.github.com/search/users?q=\(username)+repos:>100+followers:>100"