Friday, May 31, 2013

NSArray isEqualToArray example in Objective C (iOS).

NSArray isEqualToArray

Compares the receiving array to another array.

- (BOOL)isEqualToArray:(NSArray *)otherArray

An array.

Return Value
YES if the contents of otherArray are equal to the contents of the receiving array, otherwise NO.

Discussion of [NSArray isEqualToArray]
Two arrays have equal contents if they each hold the same number of objects and objects at a given index in each array satisfy the isEqual: test.

NSArray isEqualToArray example.
NSArray *origArray = @[obj1, obj2, oj3];
NSMutableArray *newMutableArray = nil;
newMutableArray = [NSMutableArray arrayWithArray:originalArray]; 
thereIsSomeDifference= ![newMutableArray isEqualToArray: [originArray mutableCopy] ];

Example of [NSArray isEqualToArray].
initA = [NSMutableArray arrayWithArray:originalArray]; 
initB = [originalArray mutableCopy];

if (initA == initB) {
   // unreachable, because the pointers differ

// however
if ([initA isEqualToArray:initB]) {
   // will be true
   // because
   for (int i=0; i        if ([initA objectAtIndex:i] == [initB objectAtIndex:i]) {
           NSLog(@"this will log every element %@ in the arrays", [initA objectAtIndex:i]);

NSArray isEqualToArray example.
NSArray *arr1 = [[NSArray alloc]initWithObjects:@"a2223a",@"ab33b",@"a1acdf",@"ac23c45", nil];
    NSArray *arr11 =  [arr1 sortedArrayUsingSelector:@selector(localizedCompare:)];
    NSArray *arr2 = [[NSArray alloc]initWithObjects:@"ab33b",@"ac23c45",@"a1acdf",@"a2223a", nil];
    NSArray *arr22= [arr2 sortedArrayUsingSelector:@selector(localizedCompare:)];
    if([arr11 isEqualToArray:arr22])
        NSLog(@"Not equal");

End of NSArray isEqualToArray example article.