在Objective-C比较阵列 [英] comparing arrays in objective-c

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

问题描述

OK了pretty简单的问题。在C ++中,似乎工作,但在Objective-C的我似乎与它的斗争:秒。
如果要比较两个数组就应该是这样的权

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 ?

推荐答案

这是可可对象数组(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的对象,所以这code应该给你一个编译器错误。我可能记错,虽然。如果它确实使其运行时,即code会崩溃迟早的事情,因为你所访问超出了数组对象的两端内存。

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 :消息,因此,在这个问题的code实际上现在的工作,但它仍然不是简单地走一个数组,更不用说来比较两个数组的正确方法)

(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中也响应 objectEnumerator reverseObjectEnumerator ,它返回一个同样-迭代对象。在这两者中, reverseObjectEnumerator 是新的code比较有用的,因为你可以在阵列上直接循环迭代前进。他们两人都是前快枚举存在的最有用的;即code是这样的:

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])) {
    …
}

(是的,这是在条件的分配。故意,因此额外的()。我们codeD大胆回来以后,我们没有?)

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

有关你在做什么,但是,你很可能要发送的阵列之一的 isEqualToArray:消息,如Williham托特兰建议:

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:消息返回; NO 其它。所述阵列可包含不同的对象,但只要各对是相等的,阵列本身是相等的。

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.

这是假设你要反对平等。两个不同的对象是平等的,如果他们中的一个与响应YES 当你发送一个的isEqual:消息,并传给其他目的。如果你的意思比较对象的身份,那么你就需要自己做锁步循环,并使用 ==

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:是我想要的东西。

这篇关于在Objective-C比较阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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