Finding The 2 Equal Records In An Array

Discussion in 'iOS Programming' started by ahan.tm, Nov 15, 2011.

  1. ahan.tm macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #1
    Hi,

    I have an array and I need to find if 2 records are the same. I have this code so far, but it cannot tell me at which index number they are the same.

    Code:
    Code:
    marbelesreached = NO;
        for(int i=0; i< [records count] ; i++){
            if ([[[records objectAtIndex: i] valueForKey: @"marbles"] intValue] == [[[records objectAtIndex: i]valueForKey:@"marblesneeded"] intValue]) {
                marbelesreached = YES;
                break;
            }
        }
    
    What is the code to find out which index number is equal.

    Thanks
     
  2. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #2
    What version of the OS are you supporting? Before you start doing this work with for-loops, try looking through the block-based enumerators. Do you see how a method like indexOfObjectPassingTest: might give you the functionality that you need?

    Also, is there a reason that you need to use valueForKey: instead of a custom accessor method? Are you using custom objects?
     
  3. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #3
    Okay, i figured it Out:

    Here is the code:

    Code:
    for(int i=0; i< [records count] ; i++){
            if ([[[records objectAtIndex: i] valueForKey: @"marbles"] intValue] == [[[records objectAtIndex: i]valueForKey:@"marblesneeded"] intValue] && [[[records objectAtIndex: i]valueForKey:@"marblesneeded"] intValue]!= 0) {
                [[records objectAtIndex:i] valueForKey: @"prizereached"] = YES;
            }
        }
    This might change if something else changes:)

    Thanks
     

Share This Page