how to remove first character from textfield value in swift

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?’

>Solution :

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()


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

Leave a Reply