Obj-c: Comparing two arrays

If I have two arrays that themselves contain arrays, how would I compare the items in array a to see if they are in array b?

  • (IBAction)GetArrayData:(id)sender {

    int a=10;
    int b=10;
    int c=20;
    int d=20;
    int row=0;
    int col=0;

    NSMutableArray* e = [[NSMutableArray alloc] init];
    NSMutableArray* f = [[NSMutableArray alloc] init];
    NSMutableArray* t = [[NSMutableArray alloc] init];

    for(row=0; row<a; row++) {
    for(col=0; col<b; col++) {
    NSNumber* thisrow = [NSNumber numberWithInt:row];
    NSNumber* thiscol = [NSNumber numberWithInt:col];
    [t addObject:thisrow];
    [t addObject:thiscol];
    [e addObject:t];
    [t removeAllObjects];
    }
    }

    for(row=0; row<c; row++) {
    for(col=0; col<d; col++) {
    NSNumber* thisrow = [NSNumber numberWithInt:row];
    NSNumber* thiscol = [NSNumber numberWithInt:col];
    [t addObject:thisrow];
    [t addObject:thiscol];
    [f addObject:t];
    [t removeAllObjects];
    }
    }
    }

Hi,

with this syntax you probably get empty arrays.
This is more reliable


.
  for(row=0; row<a; row++) {
        for(col=0; col<b; col++) { 
            NSNumber* thisrow = [NSNumber numberWithInt:row];
            NSNumber* thiscol = [NSNumber numberWithInt:col];
            [e addObject:[NSArray arrayWithObjects:thisrow, thiscol, nil]];
        }
    }
.

you can compare NSArray either for equality with the isEqual: method
or check single objects with the containsObject: method

PS: Don’t forget to release the objects after finishing with them

Thanks Stefan, I should send you a beer (or beverage of choice) for all the help you’ve given me recently. :slight_smile:

Worked like a charm. Here’s my code in case someone else finds themselves down this road. This is part of an app where I am automatically laying out ads in a magazine but it can easily be modified for a lot of uses. :

.h file:

#import <Cocoa/Cocoa.h>

@interface AppController : NSObject{
IBOutlet id btnStart;
}

  • (IBAction)GetArrayData:(id)sender;
    @end

.m file:

@implementation AppController

  • (IBAction)GetArrayData:(id)sender {

    int a=10;
    int b=10;
    int c=20;
    int d=20;
    int row=0;
    int col=0;

    NSMutableArray* e = [[NSMutableArray alloc] init];
    NSMutableArray* f = [[NSMutableArray alloc] init];

    for(row=0; row<a; row++) {
    for(col=0; col<b; col++) {
    NSNumber* thisrow = [NSNumber numberWithInt:row];
    NSNumber* thiscol = [NSNumber numberWithInt:col];
    [e addObject:[NSArray arrayWithObjects:thisrow, thiscol, nil]];
    }
    }

    for(row=0; row<c; row++) {
    for(col=0; col<d; col++) {
    NSNumber* thisrow = [NSNumber numberWithInt:row];
    NSNumber* thiscol = [NSNumber numberWithInt:col];
    [f addObject:[NSArray arrayWithObjects:thisrow, thiscol, nil]];

      }
    

    }

    //check to see if f contains items from e
    for(NSString* thisset in f) {
    if ([e containsObject: thisset]) {
    NSLog(@“This set (%@) is already being used.”, thisset);
    } else {
    NSLog(@“We can start the ad at these coordinates: %@”, thisset);
    break;
    }
    }

[e release];
[f release];
}
@end