該函數運行良好,但始終返回相同的值,因爲我不知道如何使用objectatindex。而這個:如何在for函數中使用objectAtIndex
NSLog (@"Object at index %d is: %@", i, [appDelegate.animals objectAtIndex: i]);
返回此:
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>
這是我的全碼:其實,我的東西,在結合的功能。
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]);
}
你是什麼意思「它始終返回相同的值」?每行打印一個不同的對象... – 2011-03-20 13:49:58
你在你的App中有什麼代碼?Delegate – Andrew 2011-03-20 18:44:36
NSLog(@「%@」,aAnimal.animalName);返回總是相同的值,另一個NSLog很好,但實際上不得不打印出第一個nslog輸出的內容,但NSLog總是打印出相同的值。因爲我不使用objectAtIndex。 – Max 2011-03-20 18:52:49