Luck Yong
Luck Yong

Reputation: 111

for loop in objective c

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 addObject:[_annotation2 objectAtIndex:i]];
}

Upvotes: 0

Views: 307

Answers (3)

Nishant B
Nishant B

Reputation: 2907

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]];
}

Upvotes: 0

user457812
user457812

Reputation:

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
}

Upvotes: 3

Rob Napier
Rob Napier

Reputation: 299623

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]];
}

Upvotes: 3

Related Questions