0

I am new to programming and could not find out how I might get the variable data back out of the function I created in swift. I tried returning the variable however I get an error in the compiler.

    class ViewController: UIViewController {


    @IBOutlet var textField: UITextField!

    var userName = String()

    @IBAction func returnPressed(_ sender: Any) {

        userName = textField.text! //user input will = userName
        print("return pressed inside textfield")
        print("User name set to \(userName)")

    self.view.endEditing(true) //hides keyboard
        return (userName)
    }

The function activates via the return being button pressed. The variable userName will then equal the users input within the text field.

The problem is that outside of this function, I can no longer call on the variable userName and receive the user input value that was stored earlier. And I cant seem to get the "return" to work like ive been reading.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Chief Gen
  • 3
  • 1
  • 1
    Your userName is a property of your class ViewController, once you modify it's value that new value can be accessed anywhere within the class. You do not need to return that value – Ben Ong Dec 09 '16 at 02:47
  • Moreover your function returnPressed does not have any returnType, obviously the compiler will scream at you when you try to return anything – Ben Ong Dec 09 '16 at 02:47
  • If you are new to programming you can read up [here](http://stackoverflow.com/documentation/swift/topics) – Ben Ong Dec 09 '16 at 02:49

2 Answers2

0

The function's return statement isn't doing anything because you are not declaring the function as returning anything.

func returnPressed(_ sender : Any) -> String

That would be a function that returns a String.

In this case, you don't need a return statement because you are not capturing the function's result anywhere by calling the function manually.

If you print the value of userName elsewhere in your code after pressing Return, the variable will have captured that value, please make sure to try before saying you're sure it's not working :)

0

First of all userName is an internal variable for your class ViewController and can be accessed in every function. You have to access the variable within some function like viewDidLoad, viewWillAppear or any of your custom function within your class.

Make any function and access the variable

func test() {
    //access here userName
}

All entities in your code (with a few specific exceptions) have a default access level of internal if you do not specify an explicit access level yourself. As a result, in many cases you do not need to specify an explicit access level in your code.

Read more about Access Controls.

Also you are returning something from an IBAction of the button which don't have any return type. You will get a compiler error there.

Generally return of textfields is handled by UITextFieldDelegate. See here.

The delegate method in Swift 3 is

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    return true
}
Community
  • 1
  • 1
Rajan Maheshwari
  • 14,465
  • 6
  • 64
  • 98