简体   繁体   English

在 swift 中为 uiTextField 添加前缀

[英]add prefix to uiTextField in swift

I am trying to add country code as prefix to textField so the user can enter the rest of his phone number我正在尝试将国家代码作为前缀添加到 textField 以便用户可以输入他的电话号码的 rest

   @IBAction func phoneLogin(_ sender: Any) {
    let countryCode = "+1"
    
    guard let phoneNumber = countryCode + MobileLbl.text! else { return }
    
    if ((MobileLbl.text?.isEmpty) != nil) {
        print("Fill Your Number")
    }else {
        OTPtxt.isHidden = false
        VerifyBtn.isHidden = false
    PhoneAuthProvider.provider().verifyPhoneNumber(phoneNumber, uiDelegate: nil) { (verificationId, error) in
        if error == nil {
            guard let verifyId = verificationId else { return }
            self.def.setValue(verifyId, forKey: "verificationId")
            self.def.synchronize()
            
            print(verificationId)
        } else {
            print("Unable to get Secret verification from firebase", error?.localizedDescription)
        }
    }
    
    }
    
}

I got this error Initializer for conditional binding must have Optional type, not 'String'我收到此错误条件绑定的初始化程序必须具有可选类型,而不是“字符串”

You're force unwrapping MobileLbl.text!您正在强制MobileLbl.text! which no longer makes it optional.这不再使它成为可选的。 Take off the exclamation point so it's just MobileLbl.text .去掉感叹号,所以它只是MobileLbl.text Also have to move countryCode to another line as it isn't optional either.还必须将countryCode移动到另一行,因为它也不是可选的。

let countryCode = "+1"

guard let phone = MobileLbl.text else { return nil }

let phoneNumber = countryCode + phone

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM