@IBOutlet var firstName:UITextField?
@IBOutlet var lastName:UITextField?
let string = firstName!.text
print(string)
The output is like as below:
Optional("ohh")
How can I get the data without optional text and double quotes?
Your issue is that the text
attribute of a UITextField is an Optional - this means it must be unwrapped. To do that, you add a !
to the end, which produces a String
instead of a String?
.
You can also conditionally unwrap an optional using the syntax if let
, so here it would be
if let string = firstName!.text{
print(string) //outputs if text exists
}else{
//text didn't exist
}