NSURL 致命错误

NSURL fatal Error

我尝试让 link 在 Swift 中位智,在我插入 url 之后出现致命错误。

我的代码:

let newName:String = closest.name.stringByReplacingOccurrencesOfString(" ", withString: "&", options: NSStringCompareOptions.LiteralSearch, range: nil)
    print(closest.name)
    print(newName)
    let url:String = "waze://?q=\(newName)"

    print(url)

    let navAdd: NSURL? = NSURL(string:url)// here is the error
    let wazeApp: NSURL? = NSURL(string: "http://itunes.apple.com/us/app/id323229106")!
    print(navAdd)
    if(true){
        UIApplication.sharedApplication().openURL(navAdd!)
    }else{
        UIApplication.sharedApplication().openURL(wazeApp!)
    }

错误是:

fatal error: unexpectedly found nil while unwrapping an Optional value

你已经解包了一个可选的,但没有检查它是否不是 nil。

if let navAdd = NSURL(string:url) {
    UIApplication.sharedApplication().openURL(navAdd)   
} else if let wazeApp = NSURL(string:"http://itunes.apple.com/us/app/id323229106"){
    UIApplication.sharedApplication().openURL(wazeApp)
} else {print("Url not found")}