开发者

for loop in objective c

开发者 https://www.devze.com 2023-03-14 03:09 出处:网络
Can someone help take a look at this method and advise please.. SOmethings seems not quite right. for(int i = 0; i<sizeof(_annotation2) - 5 ; i++){

Can someone help take a look at this method and advise please.. SOmethings seems not quite right.

for(int i = 0; i<sizeof(_annotation2) - 5 ; i++){
    [a.annotationsPatients add开发者_如何学运维Object:[_annotation2 objectAtIndex:i]];
}


You cannot use sizeof() to get the number of elements in an NSArray. You use count.

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


You're using sizeof incorrectly. sizeof gets the size of a given data type, not the length of what I assume is an NSArray. To get the length of an NSArray, use the count method, like so:

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


Actually "count" method is used instead of "sizeof".

Review your code as below:

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

精彩评论

暂无评论...
验证码 换一张
取 消