无法将“无效”类型的值分配给“字符串?”

时间:2020-03-04 08:59:12

标签: swift string

我遇到错误“无法将'Void'类型的值分配给'String吗?'” 我的代码

       func showPhone(place: Place){
           if let phone = place.phone {
            var urlPhone: NSURL = URL(string: "TEL://\(phone)")! as NSURL
            phoneValue.text = UIApplication.shared.open(urlPhone as URL, options: [:], completionHandler: nil)
           }
           else {
                self.phoneContainer.isHidden = true
           }
       }

1 个答案:

答案 0 :(得分:0)

原因:

您正在尝试将open(_:options:completionHandler:)的结果分配为phoneValue's text。但是,方法open(_:options: completionHandler:)不返回任何内容。这就是它给出错误的原因。

解决方案:

showPhone(place:)方法应类似于

func showPhone(place: Place){
    if let phone = place.phone {
        phoneValue.text = phone
        if let urlPhone = URL(string: "TEL://\(phone)") {
            UIApplication.shared.open(urlPhone, options: [:], completionHandler: nil)
        }
    }
    else {
        self.phoneContainer.isHidden = true
    }
}