I was building a Mac App using Swift. I have Email as TextField and Password as SecureField. I need to get the values of email and password which I entered after running the code.
Now I was trying as
@IBOutlet var Email: NSTextField!
@IBOutlet var Password: NSSecureTextField!
@IBAction func signin(sender: AnyObject) {
println("Email is: \(Email.objectValue) and password is: \(Password.objectValue)")
}
It returns me the output as
Email is: Optional([email protected]) and password is Optional(password)
I need to get only the values like
Email is: [email protected] and Password is: password
I mean, I should not get the "Optional" in the output.
TextField in SwiftUI is a simple control that shows an editable text interface (equivalent to UITextField in UIKit). Because TextField allows a user to type text, it also needs a way of storing the entered text in a State variable which can then be used to read the input.
In the same context on iOS, the text field uses either the prompt or label as placeholder text, depending on whether the initializer provided a prompt. The following example shows a Form with two text fields, each of which provides a prompt to indicate that the field is required, and a view builder to provide a label:
Because TextField allows a user to type text, it also needs a way of storing the entered text in a State variable which can then be used to read the input. The appearance of TextField can further be customized by using TextFieldStyle.
If the value is a string, the text field updates this value continuously as the user types or otherwise edits the text in the field. For non-string types, it updates the value when the user commits their edits, such as by pressing the Return key.
It seems like you do not want the objectValue
but the stringValue
of the fields.
println("Email is: \(Email.stringValue) and password is: \(Password.stringValue)")
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