Friday, January 19, 2018

#1 2010-11-04 12:22:45 pm

RIRedinPA
Member
From:: Philadelphia
Registered: 2003-11-12
Posts: 237
Website

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];
        }
    }
}


Filed under: NSArray, comparing

Offline

 

#2 2010-11-04 12:51:50 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11483
Website

Re: Obj-c: Comparing two arrays

Hi,

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

Applescript:


…
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

Last edited by StefanK (2010-11-04 12:52:35 pm)


regards

Stefan

Offline

 

#3 2010-11-05 07:37:33 am

RIRedinPA
Member
From:: Philadelphia
Registered: 2003-11-12
Posts: 237
Website

Re: Obj-c: Comparing two arrays

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

Offline

 

#4 2010-11-05 08:29:08 am

RIRedinPA
Member
From:: Philadelphia
Registered: 2003-11-12
Posts: 237
Website

Re: Obj-c: Comparing two arrays

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

Last edited by RIRedinPA (2010-11-05 08:29:53 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)