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

iOS

"Subtracting" strings in textfields

In my app i have got a variable named text that stores values from 10 textfields. Just like this: var text = textfield1.text! + textfield2.text! + textfield3.text! + textfield4.text! + textfield5.text! + textfield6.text! + textfield7.text! + textfield8.text! + textfield9.text! + textfield10.text!

A textfield can only hold one letter. If a textfield is not empty i want to disable interaction. I have managed this by doing this

         let textfields = [array of all the textfields]
         for textfield in textfields {
         if textfield.text?.isEmpty == false {
         textfield.userInteractionEnabled = false
         }
         }

but i also want to delete/remove/subtract the not empty ones from the variable text. Like I can write "sock" in the first four textfields and push the button, then get a notifications saying you wrote "sock." Then if i wrote for example "eye" in the next three, and pushed the button once again, it would say: you wrote "eye" and not: you wrote "sockeye"

I'm confused about a few things. In your first comments you seemed to imply that the constant textfields would be a concatenated string ( var text = textfield1.text! + textfield2.text! + textfield3.text! + textfield4.text! + textfield5.text! + textfield6.text! + textfield7.text! + textfield8.text! + textfield9.text! + textfield10.text!) but then later in your code it seemed like you want it to be an array. (An array I think makes more sense).

Also, you say a text field can only hold one letter, but then you say it's also going to contain words like "eye" and "sock".

Also textfield sounds like maybe it should be a variable not a constant if you want to be adding/subtracting things from it.

I have got a variable named text that stores values from 10 textfields. What i mean by that is this

if textfield1.text = "H", textfield2.text = "E" and textfield3.text = "Y" the variable named text would equal to "HEY"

I later create an array of all my textfield in order to create a for in loop to check which textfields are empty and which are not.

What i meant with the examples of sock and eye is this:

textfield1.text = "S" textfield2.text = "O" textfield3.text = "C" textfield4.text = "K"

then i push my main button and i get the following to display in a label: "You wrote SOCK" I have managed this. These four textfield will be enabled to interaction. But if i were to interact with the other textfields afterwards like this:

textfield5.text = "E" textfield6.text = "Y" textfield7.text = "E"

i would get: "You wrote SOCKEYE" in the label, instead of the desired result: "You wrote EYE."

Why not make textfields a variable, and then you can empty the array of one word in preparation for putting in the next set of characters. Or create a new variable. Are you collecting one letter at a time from people?

No, the user can write as many letters he wants to when it is his turn.

Thanks for your attention and ideas. I´ll take a look at it, and test it out.