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 Build a Simple iPhone App with Swift Deploying to a Device Wrapping Up!

Ross Campbell
Ross Campbell
5,410 Points

adding to the array

Hi everyone, first all of loved this course. Really informative and I feel like i've come a long way in a short space of time.

One thing i am interested in learning next is, how to add more and more facts to the array from inside the app.

I'm assuming it has something to do with adding a text field, and linking what i input into the text field to the array, but i'm still at the point where i find the documentation more than a little confusing to say the least and searches on google have yielded answers to a lot of questions, but not the ones i'm asking.

Any help, even if its just the a general hint of the direction to go in would be great.

Thank you

Ross

1 Answer

Jason Wayne
Jason Wayne
11,688 Points

There definitely are many types of patterns to add objects into an array, but the general core is still the same. Adding objects to an array still needs to go through the array's property or method.

Referring to your example, adding the text to the array.

  1. Assuming you use storyboard, create an outlet for the uitextfield (ex. theTextField).

  2. Create the property variable array (NSMutableArray *theMutableArray) --> Don't forget to instantiate self.theMutableArray = [NSMutableArray new]; --> perhaps place this in viewDidLoad

  3. Determine how you want to add this information to the array. For simplicity sake, let's create a button with an IBAction (onInsertTextFieldButtonPressed)

  4. Within the IBAction method :

NSString *theTextFromTheTextField = self.theTextField.text; [self.theMutableArray addObject: theTextFromTheTextField];

Viola! You're done.

Don't worry about getting confused in the documentation, you'll get it soon!

Ross Campbell
Ross Campbell
5,410 Points

Thank you very much Jason, i'll give this a go, hopefully start to pick it up a bit quicker now.