Im trying to convert a NSString from a text field to a standard String so that I can check if it contains a certain character.
I have tried
var nSStringText = textField.text! as NSString
var stringText = nSStringText as String
output
'NSString' is not convertible to 'String'
let myNSString: NSString = "I'm iOSDev"
let myString: String = myNSString as String
print(myString)
print(myString.isEmpty)
Casting Refer to myNSString as String
Also, You don't need to cast textField text as NSString it's by default String So, Just use
var myString: String = myTextField.text ?? ""
I Couldn't comment so Posting as Answer
NSString and String are bridged. You don't need to, and can't, cast from one to the other. As vacawama says in his comment, a UITextField's text property is already of types String.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With