NSURL 在展开一个 Optional 值时发现 nil

NSURL found nil while unwraping an Optional value

谁知道我为什么会这样

致命错误:在展开可选值时意外发现 nil

当我使用

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

| 字符不是有效的 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)!)!

Swift 4

我正在尝试:

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

而“>”和“<”字符给我错误。

解决方案:

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

哦,这是个疯狂的问题。由于 <> 它给出了一个错误

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