2011-06-21 206 views
-1

有人可以幫忙看看這種方法,並建議請.. SOmethings似乎不太正確。for loop in loop c

for(int i = 0; i<sizeof(_annotation2) - 5 ; i++){ 
    [a.annotationsPatients addObject:[_annotation2 objectAtIndex:i]]; 
} 
+2

那麼,你認爲什麼是不正確的?它應該做什麼?它究竟在做什麼,而不是它應該做什麼?編輯:Aaaaand我發現了明顯的問題。 – nil

+0

@Josh Caswell:我已經回滾了,因爲編輯是由作者完成的。讓他決定他的問題是什麼。 – JeremyP

+0

@Luck Young:如果您仍然遇到問題,請將該屬性的聲明添加到您的問題中。 – JeremyP

回答

4

不能使用sizeof()獲得在NSArray元素的數量。您使用count

for(int i = 0; i<[_annotation2 count] - 5 ; i++){ 
    [a.annotationsPatients addObject:[_annotation2 objectAtIndex:i]]; 
} 
+0

a.annotationsPatients = [[NSMutableArray alloc] init]; for(int i = 0; i <[_annotation2 count] - 5; i ++){a.annotationsPatients addObject:[_ annotation2 objectAtIndex:i]]; } NSLog(@「%i」,[a.annotationsPatients count]); a.annotationsPatient仍然打印0 ..我明白有什麼不對。 –

+1

@Luck Yong,你確定_annotation2有5個以上的對象嗎?如果小於5,顯然[a.annotationsPatients count]將包含0個對象。 – EmptyStack

+0

雅我相信..我打印出來,裏面有126個對象=( –

3

您錯誤地使用了sizeofsizeof獲取給定數據類型的大小,而不是我假定的長度爲NSArray。爲了得到一個NSArray的長度,使用count方法,像這樣:

NSUInteger arrayLength = [someArray count]; 
for (int x = 0; x < arrayLength; ++x) 
{ 
    // do whatever in here 
} 
+0

a.annotationsPatients = [[NSMutableArray alloc] init]; for(int i = 0; i <[_annotation2 count] - 5; i ++){a.annotationsPatients addObject:[_ annotation2 objectAtIndex:i]]; } NSLog(@「%i」,[a.annotationsPatients count]); a.annotationsPatient仍然打印0 ..我明白有什麼不對。 –

+0

您應該更新您的問題並顯示如何聲明'annotationsPatients'屬性。 – nil

0

其實「計數」的方法來代替「的sizeof」。

評論你的代碼如下:

for(int i = 0; i<[_annotation2 count] - 5 ; i++){ 
    [a.annotationsPatients addObject:[_annotation2 objectAtIndex:i]]; 
} 
+0

plz看看我的編輯代碼。我試過你的,但stil打印0 ... =( –

+0

嗨,請確保,計數_annotation2應超過5.請檢查它,並讓我知道在任何困難的情況下。 –