How can i get the data from Swift based IBOutlet UITextField? - ios

#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
}

Related

conditional binding must have Optional type, not 'String' [duplicate]

This question already has answers here:
Conditional Binding: if let error – Initializer for conditional binding must have Optional type
(8 answers)
Closed 3 years ago.
I understand that The text property of UILabel is optional, I tried adding an else but i'm not sure what else to add.
productData.product.productName is me getting the name of the product from the database
if var pName = productData.product.productName { //error
self.productName.text = pName
}
#IBOutlet weak var pName: UILabel!
Initializer for conditional binding must have Optional type, not 'String' is the error I get, It was working I think im not sure where its gone wrong
It states that the use of if condition is unnecessary, because the 'productName' is not optional.
You can directly add the value to your label.
self.productName.text = productData.product.productName //Product name is not optional
Optional chaining is used when you try to get a optional value.
For example, if you want to get the text of label then you'll require if condition.
if var text = self.productName.text { //Text in label is optional
print(text)
}
It seems like the compiler is complaining that productData.product.productName is NOT an Optional. The if var name = ____ construct only works on Optionals.
If you are testing for a non-empty String, you may want to check the count or isEmpty instead.

Force unwrapping (UITextField().text!) Not Crash .But (UILabel().text!) Crash

Check this please i need explain why texField.text! not crash and label.text! crash
And as we know UITextField has property open var text:String? and UILabel have open var text:String?
let texField = UITextField()
texField.text = nil
print(texField.text!) // not crash
let label = UILabel()
label.text = nil
print(label.text!) //crash
From the documentation of UITextField
Declaration
var text: String? { get set }
Discussion
This string is #"" by default.
From the documentation of UILabel
Declaration
var text: String? { get set }
Discussion
This property is nil by default.
Please note the subtle difference
As a general rule of thumb, you should never force-unwrap optional values like this.
You should use if-let or guard or any way provided by swift to handle optionals safely.
If a label has no text, then label.text will return nil.
However, if a text field has no text then textField will return "" (an empty string). That's why it will not crash if you force-unwrap the text property of a text field.
I'm not sure why it designed like this by Apple, but as I wrote above you should handle optionals safely.

How a textfield value can be used as a double in swift 4?

We have an outlet in swift 4 like this:
#IBOutlet var accured_sum: UITextField!
I declared another variable like this:
var sum: Double?
Now I want to assign Double value of accured_sum to variable sum.
How do I do that?
You can try something like
guard let text = accured_sum.text else { return }
sum = Double(text)
This is another solution, providing a default value
sum = Double(TextField.text ?? "0")
If sum is not nil, it has the double from the TextField. It is is nil: your textfield was not a double OR you never initialized sum.

UIlabel text does not show "optional" word when setting optional text?

I have been using optional a lot.I have declared a string variable as optional
var str: String?
Now i set some value in this variable as str = "hello".Now if i print this optional without unwrapping then it print as
Optional("hello")
But if i set text on the label as self.label.text = str then it just display value as
hello
Please explain why it does not show text as on label
Optional("hello")
The text property of UILabel is optional. UILabel is smart enough to check if the text property's value is set to nil or a non-nil value. If it's not nil, then it shows the properly unwrapped (and now non-optional) value.
Internally, I imagine the drawRect method of UILabel has code along the lines of the following:
if let str = self.text {
// render the non-optional string value in "str"
} else {
// show an empty label
}
I knew I've seen optionals printed in UILabel, UITextView, UITextField. Rmaddy's answer wasn't convincing.
It's very likely that there is an internal if let else so if the optional has a value then it will unwrap it and show. If not then it would show nothing.
However there's a catch!
let optionalString : String? = "Hello World"
label.text = "\(optionalString)" // Optional("Hello World")
label.text = optionalString // Hello World
let nilOptionalString : String?
label.text = "\(nilOptionalString)" // `nil` would be shown on the screen.
label.text = nilOptionalString // no text or anything would be shown on the screen . It would be an empty label.
The reason is that once you do "\(optionalVariable)" then it would go through a string interpolation and once its a String and not String? then the result of the interpolation would be shown!

Converting a UITextField String to a Double inside a UITextField Action

I have not been able to convert hoursWorked to a double inside a UITextField property I keep getting a "Cannot invoke initializer for type 'Double' with an argument list of type '(UITextField!)'"
#IBOutlet weak var hoursWorked: UITextField!
#IBAction func onEditingChange(sender: UITextField) {
var hoursWorkedAmount = Double(hoursWorked)
var salary = hoursWorkedAmount! * 25
print(salary)
}
I've tried to test everything before hand on playgrounds to get a better understanding of what I'm doing wrong but I keep getting the error.
You need to get the text field's text, not the text field itself.
Change:
var hoursWorkedAmount = Double(hoursWorked)
to:
var hoursWorkedAmount = Double(hoursWorked.text)
Better yet, change it to:
var hoursWorkedAmount = Double(sender.text)
This way your method acts on the actual text field that sent the event.
Note: I'm not fluent in Swift. You might need a ! after text.

Resources