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

Preload data into Core Data - Puns

Having worked through the core data tutorials I would like to take it one step further and preload data into the app with just the rating editible.

What would be the best way to do that? create the app, run it in the simulator and add the data then disable the Add button, or preload via an array?

6 Answers

Alex Hedley
Alex Hedley
16,381 Points

You could use a plist and load from there or as you suggested an array.

Have you seen the Crystal Ball tuts yet?

I haven't done it yet - been busy building an app lol but I will give it a go. I've decided to let the user put their own item in at this time and update it in the future.

Thanks for the pointer though :o)

I'm not sure how you would do such a thing, so I won't have any example code for such a thing... but...

Could you just have a second Core-Data Model? Then you could either just copy the entire thing, or individual NSManagedObjects into it?

Maybe in your viewWillLoad method for the entire thing, or perhaps as IBAction button somewhere in a preferences window or as an in-app purchase sort of function based on whether the user needs/wants it?

hmm I don't want to do the in-app purchase with it so happy to have it load with the app. I can do it with an array and then have a label for the editable content but I couldn't get that working - I might look at how to load an array but each item in a label instead of the tableview if that is at all possible.

For assigning a value to a UITextLabel, you'll need to set an outlet for each label in your header file of that view. Then just @synthesize them in your implementation.

Once you've got them setup, I would think that a "for loop" in that implementation file's viewDidLoad could accomplish the task of populating the text values for the UITextLabels. You can find examples of for loop statements here, if you need them.

Something similar to this might work if your UITextLabels were named myTextLabel0, myTextLabel1, etc:

for (int i = 0; i <= [myArrayOfStrings count]; i++)
{
    NSString * myTextLabelToUpdate = [NSString stringWithFormat:@"myTextLabel%@", i];  //something to concatenate the name of the UITextLabel with the value of "i" at the end of it
    myTextLabelToUpdate.text = [myArrayOfStrings objectAtIndex:i];
}

Cool thanks I'll give that a go!