拨打Btn电话号码有误

时间:2018-12-02 18:56:26

标签: ios swift

我在Swift 3 Xcode 10.1和下面的代码中设置了IBAction功能,使用后端db作为back4app,我在其中存储数据和数字 但是在点击应从列中检索数字的呼叫按钮后,它给了我这样的单词Optional(6788899) 而不是只能通过手机拨打电话的号码,是什么原因导致这个单词出现任何帮助呢?

//check for missing data
if(phone == nil || phone == 0){
    let alert = UIAlertController(title: "", message: "No phone available", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
    self.present(alert, animated: true, completion: nil)
}else{
    let url:URL = URL(string: "tel://" + String(describing:phone))!
    UIApplication.shared.open(url, options: convertToUIApplicationOpenExternalURLOptionsKeyDictionary([:]), completionHandler: nil)
}

1 个答案:

答案 0 :(得分:0)

您必须解开可选的phone

let url = URL(string: "tel://" + String(describing:phone!))!

但是,这是 swiftier 的一种实现方式。

请不要滥用String(describing:

//check for missing data
if let phoneNumber = phone, phoneNumber != 0 {
    let url = URL(string: "tel://\(phoneNumber)")!
    UIApplication.shared.open(url, options: convertToUIApplicationOpenExternalURLOptionsKeyDictionary([:]), completionHandler: nil)
} else {
    let alert = UIAlertController(title: "", message: "No phone available", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
    self.present(alert, animated: true, completion: nil)
}