swift - NSURL fatal error

标签 swift nsurl

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

相关文章:

ios - Xcode 11.3、UINavigationController 和 UITabbarController 面临的问题

ios - 屏幕旋转后如何调用sizeForItemAtIndexPath方法

ios - AppDelegate swfit4.2 中的 "applicationDidReceiveMemoryWarning"内存警告警报?

ios - containerURLForSecurityApplicationGroupIdentifier 获得零值

iphone - 将 JSON 数据放入 NSURL

swift - NSURLErrorDomain 代码=-1100

swift - 为什么将未命名的函数作为转换传递给数组会在最远的抽象中为 Playground 上的迭代计数加一

ios - 属性字符串未正确采用 kern 值

ios - 通过此方法更改NSURL字符串stringByAddingPercentEncodingWithAllowedCharacters

ios - 从常量类访问基本 url