一尘不染

解开Optional值时发现NSURL为nil

swift

谁知道我为什么

致命错误:解开Optional值时意外发现nil

当我使用

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

阅读 328

收藏
2020-07-07

共1个答案

一尘不染

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