比较objective-c中的数组 [英] comparing arrays in objective-c

查看:41
本文介绍了比较objective-c中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,一个非常简单的问题..在 c++ 中它似乎有效,但在objective-c 中我似乎在与它斗争:S ..如果你想比较两个数组,它应该是这样的

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");
  }
}

究竟是什么问题?

推荐答案

这些是 Cocoa 数组对象(NSArray 的实例),而不是 C 数组或 C++ 向量,请记住,Objective-C 没有运算符重载.您可以对对象执行的唯一操作是传递它、将其存储在变量中并向其发送消息.

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.

所以数组下标运算符对于 Objective-C 对象是错误的.我不认为取消引用指向 Objective-C 对象的指针在语言上是有效的,所以这段代码应该给你一个编译器错误.不过我可能记错了.如果它确实进入运行时,该代码迟早会崩溃,因为您正在访问超出数组对象末尾的内存.

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.

(2013 年的Objective-C 现在支持对象的下标.这最终会转化为适当的 objectAtIndex:replaceObjectAtIndex:withObject: 消息.所以,问题中的代码现在实际上可以工作,尽管它仍然不是简单地遍历数组的正确方法,更不用说比较两个数组了.)

(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.)

通过索引从 NSArray 对象中检索对象的正确方法不是使用数组下标运算符,而是向数组对象发送 objectAtIndex: 消息:

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 还响应 objectEnumeratorreverseObjectEnumerator,它们返回一个类似的可迭代对象.在这两者中,reverseObjectEnumerator 在新代码中更有用,因为您可以直接对数组进行迭代以向前迭代.在快速枚举出现之前,它们都是最有用的.该代码如下所示:

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?)

不过,对于您正在做的事情,您更有可能希望向其中一个数组发送 isEqualToArray: 消息,正如 Williham Totland 建议的那样:

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

这将确保两个数组具有相同的长度,然后以锁步方式遍历它们,将 isEqual: 发送到每对对象.如果每个 isEqual: 消息都返回 YES,它将返回 YESNO 否则.数组可能包含不同的对象,但只要每一对相等,数组本身就相等.

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.

假设您想要对象相等.当您向其发送 isEqual: 消息并传递另一个对象时,如果其中一个以 YES 响应,则两个单独的对象是相等的.如果您打算比较对象的身份,那么您确实需要自己执行锁步循环并使用 ==:

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

但这不太可能.大多数时候,我想测试对象的相等性,而不是身份,所以 isEqualToArray: 就是我想要的.

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

这篇关于比较objective-c中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆