Ashley Rodrigues Ashley Rodrigues - 2 months ago 7
Objective-C Question

How to compare two nsarrays with different values?

Hi i have two nsarrays.

Array A:

arrProductSelection = [[NSArray alloc]initWithObjects:@"English",@"German",@"Russian",@"Chinese",@"Spanish",@"French",@"French",@"French",@"French",@"French",@"French",@"French",@"French",nil];


Array B:

arrProductSelectionB = [[NSArray alloc]initWithObjects:@"deselcted",@"selected",@"selected",@"selected",@"deselcted",@"deselcted",@"deselcted",@"deselcted",@"deselcted",@"deselcted",@"deselcted",@"deselcted",@"deselcted",nil];


I need to compare two arrays and get the value from array A by comparing with array B having value as selected. That is i should get german,chinese and russian sepearted by comma as nsstring.

Answer

Try this:

NSMutableArray *arrSelected = [[NSMutableArray alloc] init];

NSArray *arrProductSelection = [[NSArray alloc]initWithObjects:@"English",@"German",@"Russian",@"Chinese",@"Spanish",@"French",@"French",@"French",@"French",@"French",@"French",@"French",@"French",nil];

NSArray *arrProductSelectionB = [[NSArray alloc]initWithObjects:@"deselcted",@"selected",@"selected",@"selected",@"deselcted",@"deselcted",@"deselcted",@"deselcted",@"deselcted",@"deselcted",@"deselcted",@"deselcted",@"deselcted",nil];

for(int i = 0; i< arrProductSelectionB.count-1;i ++) {
    if ([arrProductSelectionB[i] isEqualToString:@"selected"]) {
        [arrSelected addObject:arrProductSelection[i]];
    }
}

NSString *strSelected = [arrSelected componentsJoinedByString:@","];

NSLog(@"%@", strSelected);//output: German,Russian,Chinese