This function works well, but it always returns the same value because I don’t know how to work with the objectatindex. And this:
NSLog (@"Object at index %d is: %@", i, [appDelegate.animals objectAtIndex: i]);
Returns this:
2011-03-20 14:38:07.365 Tutorial[5063:207] Object at index 0 is: <Animals: 0x6151a20>
2011-03-20 14:38:07.367 Tutorial[5063:207] Object at index 1 is: <Animals: 0x6151ba0>
2011-03-20 14:38:07.368 Tutorial[5063:207] Object at index 2 is: <Animals: 0x6151d20>
2011-03-20 14:38:07.371 Tutorial[5063:207] Object at index 3 is: <Animals: 0x6151e90>
2011-03-20 14:38:07.374 Tutorial[5063:207] Object at index 4 is: <Animals: 0x6151ff0>
2011-03-20 14:38:07.375 Tutorial[5063:207] Object at index 5 is: <Animals: 0x6152180>
This is my full code: I actually have to combine the things in the for function.
int i = 0;
int count;
TutorialAppDelegate *appDelegate = (TutorialAppDelegate *)[[UIApplication sharedApplication] delegate];
Animals *aAnimal = (Animals *)[appDelegate.animals objectAtIndex:i];
count = [appDelegate.animals count];
for (i = 0; i < count; i++)
{
// returns the right value, but always the same because no objectAtIndex
NSLog(@"%@",aAnimal.animalName);
NSLog (@"Object at index %d is: %@",
i, [appDelegate.animals objectAtIndex: i]);
}
Have you tried:
The reason you’re seeing stuff like:
…is because you’re logging the entire object, and not just the name. But you can see that a different object is indeed being returned each time by looking at the memory address of each one (the part that looks like
0x615....).