简体   繁体   English

ios:NSArray containsObject:NSIndexPath

[英]ios: NSArray containsObject: NSIndexPath

I'm a little bit confused about NSArray containsObject method. 我对NSArray containsObject方法有些困惑。 Does this method compares pointers to objects or exactly content of them? 此方法是否比较指向对象的指针或对象的确切内容? To be more specific: i have got an array of NSIndexPath objects, created somewhere before, and now i want to check, if there is an NSIndexPath object with row = 2 and section = 1. If i write the following code, would it work? 更具体地说:我之前在某个地方创建了一个NSIndexPath对象数组,现在我要检查是否存在一个NSIndexPath对象,该对象的row = 2和section =1。如果我编写以下代码,是否可以工作?

[[self arrayOfPathes] containsObject: [NSIndexPath indexPathForItem: 2 inSection: 1]];

I've tested that code, and it worked for me, but i had some bad experience with comparison of pointers instead of object's content beforehand (btw, that code worked for me, and was approved for the app store, but late many of users reported about the bug made by it). 我已经测试了该代码,并且对我有用,但是我事先对指针而不是对象的内容进行比较有不好的经验(顺便说一句,该代码对我有用,并且已被批准用于应用商店,但后来有很多用户报告有关它造成的错误)。

Did you try looking in the docs for the NSArray method containsObject? 您是否尝试过在文档中查找NSArray方法containsObject? It says: 它说:

This method determines whether anObject is present in the array by sending an isEqual: message to each of the array's objects (and passing anObject as the parameter to each isEqual: message). 该方法通过向每个数组对象发送isEqual:消息(并将anObject作为参数传递给每个isEqual:消息)来确定anObject是否存在于数组中。

So if the object in question implements isEqual to compare the setting of the object instead of comparing the address, it will work. 因此,如果有问题的对象实现isEqual来比较对象的设置而不是比较地址,则它将起作用。

The docs on NSIndexPath don't tell you if it implements a custom implementation of the isEqual method or not, but I would guess that it does. NSIndexPath上的文档没有告诉您是否实现了isEqual方法的自定义实现,但我想它确实实现了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM