Home > Software engineering >  how to remove first character from textfield value in swift
how to remove first character from textfield value in swift

Time:09-14

if my textfield takes 11 characters then i need to remove first character and then pass it to jason perameter, so i tried like below

var dropFirst: String?
if emailPhoneTextField.text?.count == 11{
    dropFirst = emailPhoneTextField.text?.dropFirst()
        emailPhoneTextField.text = dropFirst 
    }

then o/p: then error, how to fix

Cannot assign value of type 'String.SubSequence?' (aka 'Optional') to type 'String?'

CodePudding user response:

dropFirst returns SubSequence so you can't assign it directly to textfield's text property that accepts an optional string (String?) , So replace

dropFirst = emailPhoneTextField.text?.dropFirst()

With

dropFirst = String(emailPhoneTextField.text!.dropFirst())
  • Related