If my textfield takes 11 characters and I need to remove first character and then pass it as a parameter. I tried this code:
var dropFirst: String?
if emailPhoneTextField.text?.count == 11{
dropFirst = emailPhoneTextField.text?.dropFirst()
emailPhoneTextField.text = dropFirst
}
I receive this error:
Cannot assign value of type ‘String.SubSequence?’ (aka ‘Optional’) to type ‘String?’
2
Answers
dropFirst returns
SubSequence
so you can’t assign it directly to textfield’stext
property that accepts an optional string (String?
) , So replaceWith
Create String extension to assign
SubSequence
toString
property.