views:

1344

answers:

4

Ok a pretty simple question.. in c++ it seems to work but in objective-c i seem to struggle with it :S .. If you want to compare two arrays it should be something like this right

for ( int i = 0; i < [appdelegate.nicearray count]; i++ ) 
{ 
  if ( appdelegate.nicearray[i] == appdelegate.exercarray[i] )
  { 
     NSLog(@"the same elements in this selection");
  }
}

what's the problem exactly ?

+1  A: 

Try telling us the result you're getting when you run this code. The approach is correct, but try this one:

for (int i =0; i< appdelegate.nicearray.count; i++)
{
    if ([[appdelegate objectAtIndex:i] isEqual: [appdelegate.exercarray objectAtIndex:i]])
    {
        NSLog(@"the same");
    }
}
Dan Lorenc
You should generally compare objects with `isEqual:`, not pointer equality.
Chuck
Moreover, you could just use `isEqualToArray:` to do this, as Williham Totland suggested. (Props for pointing out that he should be using `objectAtIndex:` instead of the array-subscript operator, though.)
Peter Hosey
+6  A: 

You want the isEqualToArray: method. As in:

if ([arrayOne isEqualToArray:arrayTwo]) {
  // Do something
}

This will recursively compare the two arrays, while having the advantage of not being needlessly circuitous and not requiring a loop.

Williham Totland
A: 

I do the following when comparing arrays:

  • Check to see if any of the arrays are nil when the other is not
  • Check to see if the lengths are the same
  • Iterate (using a for loop like you have) over each element checking the matching element in the other array.

To compare elements you need to define what you want to regard as being "equal". Are they equal only if the pointers in the array are equal or can they be equal if the content is equal too.

For the pointer case, you can use ==.

For the deep comparison you might need to use CompareTo or something similar.

rein
+4  A: 

These are Cocoa array objects (instances of NSArray), not C arrays or C++ vectors, and remember that Objective-C does not have operator overloading. The only things you can do with an object are pass it around, store it in variables, and send messages to it.

So the array-subscript operator is wrong with Objective-C objects. I don't think it's even linguistically valid to dereference a pointer to an Objective-C object, so this code should be giving you a compiler error. I may be misremembering, though. If it does make it to runtime, that code will crash sooner or later, since you're accessing memory beyond the ends of the array objects.

The proper way to retrieve an object from an NSArray object by its index is not to use the array-subscript operator, but to send the array object the objectAtIndex: message:

[myArray objectAtIndex:i]

The proper way to iterate on the elements of an array object, assuming you don't really need the index for something else (such as replacing objects in a mutable array), is to loop on it directly (this is called “fast enumeration”):

for (MyObject *myObject in myArray) {
    …
}

NSArray also responds to objectEnumerator and reverseObjectEnumerator, which return a similarly-iterable object. Of the two, reverseObjectEnumerator is the more useful in new code, since you can just iterate on the array directly to iterate forward. Both of them were most useful before fast enumeration existed; that code looked like this:

NSEnumerator *myArrayEnum = [myArray objectEnumerator];
MyObject *myObject;
while ((myObject = [myArrayEnum nextObject])) {
    …
}

(Yes, that's an assignment in the condition. Deliberately, hence the extra (). We coded boldly back then, didn't we?)

For what you're doing, though, you more likely want to send one of the arrays an isEqualToArray: message, as Williham Totland suggested:

BOOL theyAreEqual = [myFirstArray isEqualToArray:mySecondArray];

This will make sure both arrays have the same length, then walk them both in lock-step, sending isEqual: to each pair of objects. It'll return YES if every isEqual: message returned YES; NO otherwise. The arrays may contain different objects, but as long as each pair is equal, the arrays themselves are equal.

That assumes you want object equality. Two separate objects are equal if one of them responds with YES when you send it an isEqual: message and pass the other object. If you meant to compare the identities of the objects, then you do need to do the lock-step loop yourself and use ==:

BOOL arraysContainTheSameObjects = YES;
NSEnumerator *otherEnum = [otherArray objectEnumerator];
for (MyObject *myObject in myArray) {
    if (myObject != [otherEnum nextObject]) {
        //We have found a pair of two different objects.
        arraysContainTheSameObjects = NO;
        break;
    }
}

But that's unlikely. Most of the time, I have wanted to test the objects' equality, not identities, so isEqualToArray: is what I wanted.

Peter Hosey
+1 for teaching a man to fish.
willc2