How to work with an objectAtIndex in a for function
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 = (TutorialAppDelegat开发者_JAVA技巧e *)[[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:
for (i = 0; i < count; i++) {
Animals* nextAnimal = (Animals*)[appDelegate.animals objectAtIndex: i];
NSLog (@"Object at index %d is: %@",
i, nextAnimal.animalName);
}
The reason you're seeing stuff like:
1-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>
...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....
).
There's nothing actually wrong with your code - so I'm not sure what you're tying to do. You're printing out the actual pointer to the console, so you're seeing the memory locations of each object in the array.
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>
Do you see how the two objects are different? Object 0 is located at 0x615*a20*, and Object 1 is located at 0x6151*ba0*. What you want is to print out something useful from that object, which I can't really give you any help on as I don't know what your custom object containts.
It's a bit unclear what's going on in your code, but if you have an array (and I assume, since you're using objectAtIndex:
, you do), it's much better to iterate through it as follows:
for (id object in myArray) {
NSLog(@"I am: %@", [object description]);
}
That syntax is Apple's recommended way to iterate through an NSArray. It adds more protection against the array changing while you're iterating through it.
If you want the NSLog description to be more informative, override -description in your class. for example
-(NSString *)description
{
NSString *description = [super description];
description = [description stringByAppendingFormat:@" %@ %@", self.name, self.age];
return description;
}
Then, when NSLog will call the Animal's description method, you'll see whatever properties you choose to print out.
精彩评论