Using NSArray for C-type int comparisons

Posted by Andrew Barinov on Stack Overflow See other posts from Stack Overflow or by Andrew Barinov
Published on 2011-11-15T01:35:25Z Indexed on 2011/11/15 1:52 UTC
Read the original article Hit count: 183

Filed under:
|
|
|

I would like to iterate through an NSArray of ints and compare each one to a specific int. All ints are C type ints.

The code I am using is as follows:

-(int)ladderCalc: (NSArray*)amounts: (int)amount 
{
    int result;

    for (NSUInteger i=0; i< [amounts count]; i++) 
    {
        if (amount < [amounts objectAtIndex:i]);
        {
            // do something
        }
        // do something
    }
}

However I get an error when comparing the int amount to the result of [amounts objectAtIndex:i] because you cannot compare id to int. Why is the id involved in this case? Shouldn't objectAtIndex just return the object at the index specified? Is it possible to cast the object returned to an C int and then do the comparison?

Or should I just dispense with NSArray and do this type of thing in C?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about c