需要一些帮助。这是我的代码,当我单击 GoMap 按钮时,系统提示我此错误 fatal error :在解包可选值时意外发现 nil
@IBAction func GoMap(_ sender: UIButton) {
UIApplication.shared.openURL(NSURL(string: "http://maps.apple.com/maps?saddr=1 Republic Boulevard , Singapore 038975")! as URL)
}
最佳答案
您的网址无效。空格需要转义为 %20:
URL(string: "http://maps.apple.com/maps?saddr=1%20Republic%20Boulevard,%20Singapore%20038975")!
或者,使用 URLComponent + URLQueryItem 让系统为您转义:
var components = URLComponents(string: "http://maps.apple.com/maps")!
components.queryItems = [URLQueryItem(name: "saddr", value: "1 Republic Boulevard, Singapore 038975")]
let url = components.url!
UIApplication.shared.openURL(url)
关于ios - Xcode 8 打开新的苹果 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42053395/