gpt4 book ai didi

swift - NSURL fatal error

转载 作者:行者123 更新时间:2023-11-30 13:57:03 25 4
gpt4 key购买 nike

在插入网址后,我尝试在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com