简体   繁体   中英

ios: NSArray containsObject: NSIndexPath

I'm a little bit confused about NSArray containsObject method. 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?

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

So if the object in question implements isEqual to compare the setting of the object instead of comparing the address, it will work.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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