0

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?

1
  • 1
    What is records? An array of NSDictionary entries? What does "all entries of the array at one time" mean--in more detail please. Commented Nov 12, 2011 at 17:01

2 Answers 2

3

Try something like this -

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

if([marblesArray isEqualToArray:marblesNeededArray]){
// do something
}
Sign up to request clarification or add additional context in comments.

7 Comments

This might work, but it might not work too. As the objects in the array have to respond to the isEqual: method. Also, if the objects have other fields than marbles and marblesneeded, then these field have to be equal too, otherwise this code might not work even if the two fields of interest are always equal. Hence, this can work only if the data structures in the records array obey certain constraints.
@simonpie... I don't agree with you on this point - Also, if the objects have other fields than marbles and marblesneeded, then these field have to be equal too.
@simonpie ... what I am doing over here is making two different arrays from records array and comparing them .. so need to worry about the situation you specified.
@miraaj... Then I am confused in why you do not agree. The question only wants to compare two given fields and act on those. Now, if the other fields are not equal for even one element but marbles and marblesneeded are equal for that element, the code you propose will branch to the else clause. From what I understand, it would no be equivalent to what is asked. Unless I do not understand correctly your code.
I am sorry, I missread your code. This will work. I read to fast and did not see te two NSArray you were creating.
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.