Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Variable Name

How would I make a variable which is named what the user inputed in the UITextField, and is equal to what the user inputed in the text field using swift.. Stone Preston Stephen Whitfield

1 Answer

Filipe Alvarenga
Filipe Alvarenga
5,478 Points

Hello Steven,

If I understood, in your first question you wants to name a variable dynamically based on an user input, right?

So, I think that it is not possible, because your variable name is defined before the app is compiled and the user input is provided in runtime.

For your second question, about store an UITextField string inputed by the user in a variable using Swift, I think that the code below will help you:

var userInput = self.myUITextFieldIBOutlet.text

If you wants to guarantee that the user has provided an input you can check if the UITextField is empty, try this code:

if (self.myUITextFieldIBOutlet.text.length == 0) {

println("The user not entered any text in the UITextField")

} else {

//You can store the user input in the variable
var userInput = self.myUITextFieldIBOutlet.text

}

I hope that my answer helped you!

Best Regards,

(Sorry if I made any grammar mistake, I'm just learning english :))

Thanks. When I click on a tableview cell, I made it so I am taken to another viewcontroller. Do you know how I can reference the value that was in the tableview cell in the other viewcontroller. Filipe Alvarenga