сравнение массивов в цели-c

Это производит счетную последовательность. То, что это делает, на самом деле создает локальную последовательность IEnumerable и возвращает его как результат метода

12
задан hakre 8 May 2013 в 13:05
поделиться

4 ответа

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.

(EDIT from the year 2013: Objective-C now supports subscripting of objects. This ultimately translates into the appropriate objectAtIndex: or replaceObjectAtIndex:withObject: message. So, the code in the question would actually work now, although it's still not the proper way to simply walk an array, much less to compare two arrays.)

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.

41
ответ дан 2 December 2019 в 02:56
поделиться

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.

19
ответ дан 2 December 2019 в 02:56
поделиться

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.

0
ответ дан 2 December 2019 в 02:56
поделиться

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");
    }
}  
1
ответ дан 2 December 2019 в 02:56
поделиться