views:

21

answers:

1

This is the Core Data object model I am working with (I am a new user, so I can't post pictures directly):

http://i965.photobucket.com/albums/ae134/spindler77/Screenshot2010-07-26at112231AM.png

All I am trying to do right now is to populate a UITableView with the "className" of every myClass instance the user has created and saved.

My code for generating the myClass instances is fully functioning, but what I can't figure out is how to extract the "className" attribute (to put in my tableview cells) once I have fetched the myClass objects. I have read and re-read the Apple docs (though I'm sure the "answer" is there somewhere, I'm just having a hard time deciphering it).

I have scoured Core Data tutorials as well. The most helpful one that I found has only gotten me to a point where my managed objects are stored in an array, but I can't figure out how to access the attributes of those objects.

My question: How do I access a specific attribute of every managed object (instance) of a given entity?

Thanks in advance for your help.

-Michael

+1  A: 

Once you've fetched all your objects into an NSArray, you can then just iterate through the array, for example using fast enumeration:

for (myClass *cl in array) {
    NSLog(@"Name = %@", cl.className);
}
David Gelhar
That sounds right, but with that code I'm getting a warning that says, "Passing argument 1 of 'NSLog' from incompatible pointer type" and the program quits with a EXC_BAD_ACCESS at that line.
Spindler
Oops, I left out the '@' in front of the string constant. NSLog expects an NSString format string, so it needs to be `@"Name = %@"` instead of `"Name = %@"` (without the '@', you get a C string, not an NSString). I'll correct the example.
David Gelhar
Ah, I should have spotted that :P Alright, I'm almost there. Now I'm just not sure how to implement that in my cellForRowAtIndexPath. I want to access my array once for each indexPath. Currently, I have two classes stored, so I want to access it at array[0] and array[1] and extract the className from each. The "myClass *cl in array" format doesn't seem to work in that case. I tried "for (myClass *cl in array[indexPath]), but no luck.
Spindler
If you just want a single element from the array, you do something like `[array objectAtIndex:0]`
David Gelhar
Thanks David, your input is very helpful. However, I may be asking the wrong question. I think I should be using an NSFetchedResultsController instead of fetching into an array. I will post a separate question about that, but as far as this question is concerned, you answered it. The iteration you provided works perfectly.
Spindler
Yes, I definitely recommend an NSFetchedResultsController for the case of displaying all your instances in a UITableView
David Gelhar