简体   繁体   中英

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

   @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! which no longer makes it optional. Take off the exclamation point so it's just MobileLbl.text . Also have to move countryCode to another line as it isn't optional either.

let countryCode = "+1"

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

let phoneNumber = countryCode + phone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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