Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


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