Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get TextField Value using Swift to make Mac App

Tags:

macos

swift

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.

like image 312
Rocking Me Avatar asked Dec 18 '14 12:12

Rocking Me


People also ask

How do you use textfield in SwiftUI?

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.

What is the placeholder text for the text field on iOS?

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:

Why does textfield need a state variable?

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.

How do I update a text field value?

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.


1 Answers

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)")
like image 70
Thorsten Karrer Avatar answered Sep 21 '22 06:09

Thorsten Karrer