Ahan Malhotra
Ahan Malhotra

Reputation: 185

Compare every object of an array with every other object

I am trying to crate an if statement like this:

if ([[records objectAtIndex:ANYPAGE] valueForKey: @"marbles"] intValue] == 
    [[[records objectAtIndex:ANYPAGE] valueForKey:@"marblesneeded"] intValue]) 
{
    // Some Code Goes Here. . .
}

I want to be able to check if "marbles" and "marbles needed" are the same in all entries of the array at one time. How can I accomplish this?

Upvotes: 0

Views: 1454

Answers (2)

simonpie
simonpie

Reputation: 354

Any code you can write will loop through the array either directly or indirectly like this :

BOOL allEqual = True;
    for(int i=0; i< [records count] ; i++){
        if ([[records objectAtIndex: i] valueForKey: @"marbles"] intValue] != [[[records objectAtIndex: i]          valueForKey:@"marblesneeded"] intValue]) {
            allEqual = False;
            break;
        }
    }
//do what ever using allEqual

Upvotes: 1

Devarshi
Devarshi

Reputation: 16758

Try something like this -

NSArray * marblesArray = [records valueForKey: @"marbles"];
NSArray * marblesNeededArray = [records valueForKey: @"marblesneeded"];

if([marblesArray isEqualToArray:marblesNeededArray]){
// do something
}

Upvotes: 3

Related Questions