链接不工作......为什么?

时间:2017-08-14 19:50:59

标签: ios swift url swift3

链接无法正常工作:

let MoovitURL: String = "moovit://nearby?lat=40.758896&lon=-73.985130&title=\(barNameTemplate)&partner_id=<nightout>"

        let escapedString = MoovitURL.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
        UIApplication.shared.openURL(URL(string: escapedString!)!)

话说:

fatal error: unexpectedly found nil while unwrapping an Optional value
2017-08-14 22:49:35.983585+0300 TestApp[10658:6538686] fatal error: unexpectedly found nil while unwrapping an Optional value

在线崩溃:

UIApplication.shared.openURL(URL(string: MoovitURL)!)

1 个答案:

答案 0 :(得分:-1)

Swift并不认为你的字符串是一个实际的URL,所以它返回nil。当您试图强行打开它(!)时,它会出错。