14

What is the diference between these two methods belonging to the NSSet class:

-(BOOL)containsObject:(id)anObject
-(id)member:(id)object 
j0k
  • 22,600
  • 28
  • 79
  • 90
NSExplorer
  • 11,849
  • 12
  • 49
  • 62

1 Answers1

20

The answer lies in the return values. containsObject returns a YES or a NO depending on if the object you send belongs to that particular set.

member returns id, which means that it returns the actual object if that object is part of the set.

As an example, you have an NSSet, aSet, with anObject. anObject belongs to the set.

[aSet containsObject:anObject]; //returns YES
[aSet member:anObject]; //If the set contains an object equal to object (as determined by isEqual:) then that object (typically this will be object), otherwise nil.

If anObject does not exist in aSet:

[aSet containsObject:anObject]; //return NO
[aSet member:anObject]; //return nil
sosborn
  • 14,676
  • 2
  • 42
  • 46
  • 7
    I'd bet that the return value of `member:` might *not* be `anObject`. That is, if the set contains an object that `isEqual:` to the argument, the set's object will be returned. – bbum Jul 04 '11 at 03:02
  • @bbum - yes, that is definitely the case. I'll update the answer to make it more clear. – sosborn Jul 04 '11 at 03:55
  • 2
    But think the real question here is whether or not containsObject: also uses isEqual: to determine the result, or is it uses isIdenticalToObject:. – Avi Shukron Oct 09 '11 at 16:04
  • 1
    @AvrahamShukron containsObject in fact uses isEqual: if the underlying class has implemented it. – Kaan Dedeoglu Oct 03 '13 at 08:30