在插入网址后,我尝试在 Swift 中链接到位智,但出现 fatal error 。
我的代码:
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
最佳答案
您已经打开了一个 optional ,但没有检查它是否不为零。
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")}
关于swift - NSURL fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33486025/