0

[NSArray containsObject] use isEqual. isEqual default to the address.

Say I want to know if [NSArray containsObject] based on reference? So I want to know whether that actual object, rather than an object eQual to it, is in the array.

I want to use it for to compare core data objects. Core data objects may be deleted and dereferenced and I wonder what happen to all array that contain elements that reference it.

Anonymous White
  • 2,149
  • 3
  • 20
  • 27

1 Answers1

2

isEqual default to the address

And in that case, is there a problem?

It sounds like you want to store objects where isEqual does not use the address, but still use the address for this check, right?

There's no -[containsObjectIdenticalTo:], but there is an -[indexOfObjectIdenticalTo:]; if that returns anything but NSNotFound, it's contained.

if ([anArray indexOfObjectIdenticalTo:anObject] != NSNotFound) {
}

As the NSArray reference docs make clear:

Objects are considered identical if their object addresses are the same.

There's a whole family of Identical methods on NSArray and other collections, that are there exactly for this purpose.

abarnert
  • 354,177
  • 51
  • 601
  • 671