2011-08-31 167 views

回答

2

這應該做的伎倆:

NSInteger max, i = 0; 
max = [mainArray count] < 50 ? [mainArray count] : 50; 

while (i < max) { 
    NSDictionary *crossArrayDictionnary = [mainArray objectAtIndex:i]; 
    // You code here. 
    i++; 
} 
+0

非常聰明。採用 –

+0

你知道什麼是最有效的方法嗎?該還是那個時候? –

+1

''用'in數組'語句,因爲它會使用快速枚舉。 – rckoenes

0

不知道Objective-C的,但這樣的事情:

int i=0; 
while (i < 50 && i < [mainArray count]) { 
    NSDictionary *crossArrayDictionnary = [mainArray objectAtIndex:i]; 
    //some code... 
    i++; 
} 
+1

不能得到一個項目與[]你需要調用objectAtIndex。 – rckoenes

+0

謝謝。每天學些新東西。 – Botz3000

0

正如rckoenes說,
微小變化。

NSInteger i = 0; 
while (i < 50 && [itemsMutable cout] >= i) { 
    NSDictionary *crossArrayDictionnary = [itemsMutable objectAtIndex:i]; 
    // You code here. 
    i++; 
} 
+1

它更好地做手動計數而不是if,這會在每次循環時調用計數。 – rckoenes

2
NSEnumerator *enumerator = [mainArray objectEnumerator]; 
NSDictionary *crossArrayDictionnary = nil; 

while ((crossArrayDictionnary = [enumerator nextObject])) { 

    //some code... 
    i++; 
    if (i>=50) { 
     break; 
    } 
}