• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
-[NSMutableSet addObject:] Ambiguous Docs: -isEqual: vs. ==
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

-[NSMutableSet addObject:] Ambiguous Docs: -isEqual: vs. ==


  • Subject: -[NSMutableSet addObject:] Ambiguous Docs: -isEqual: vs. ==
  • From: Jerry Krinock <email@hidden>
  • Date: Wed, 15 Oct 2008 16:45:21 -0700

Documentation for -[NSMutableSet addObject:] says:

"Adds a given object to the receiver, if it is not already a member. ... If anObject is already present in the set, this method has no effect on either the set or anObject."

The terms "a member" and "present" are not defined and there are two possible meanings in the Cocoa context: "-isEqual:" or " == ".

My first guess was that it meant " == " but I did a test [1] and found that I was wrong. My test result showed the behavior to be that of "- isEqual:".

I am assuming therefore assuming "-isEqual:" in writing my code. Someone please let me know if they see any risk in that.

Yes, I've sent feedback to Apple about the imprecise documentation.

Jerry Krinock


[1]

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSMutableSet* ms = [NSMutableSet set] ;

    // Produce two strings that are "-isEqual"
    // but not == .
    NSInteger r1 = random() ;
    NSInteger r2 = random() ;
    NSInteger rr1 = r1/2048 + r2/2048 ;
    NSInteger rr2 = r1/2048 + r2/2048 ;
    NSString* s1 = [NSString stringWithFormat:@"%d", rr1] ;
    NSString* s2 = [NSString stringWithFormat:@"%d", rr2] ;
    NSLog(@"s1: %p %@", s1, s1) ;
    NSLog(@"s2: %p %@", s2, s2) ;
    // Unless the compiler is REALLY smart, it does not
    // know that s1 and s2 are "-isEqual", without in
    // fact invoking -isEqual.

    // Now add both of them to as set
    [ms addObject:s1] ;
    [ms addObject:s2] ;
    NSLog([ms describeInDetail]) ;
    [ms removeAllObjects] ;

    [pool drain] ;
    return 0;
}

@interface NSSet (DetailDescription)

- (NSString*)describeInDetail ;


@end

@implementation NSSet (DetailDescription)

- (NSString*)describeInDetail {
    NSMutableString* dd = [NSMutableString string] ;
    [dd appendFormat:@"Set has %d objects:", [self count]] ;
    for (id s in self) {
        [dd appendFormat:@"   %p %@", s, s] ;
    }

    return dd ;
}

@end

****** OUTPUT ******

s1: 0x1067b0 1294540
s2: 0x107180 1294540
Set has 1 objects:   0x1067b0 1294540


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: -[NSMutableSet addObject:] Ambiguous Docs: -isEqual: vs. ==
      • From: Nick Zitzmann <email@hidden>
  • Prev by Date: Re: -[NSGarbageCollection disableCollectorForPointer:] ?
  • Next by Date: Re: NSBezierPath problems, seems to be two bugs
  • Previous by thread: Re: NSView subclass
  • Next by thread: Re: -[NSMutableSet addObject:] Ambiguous Docs: -isEqual: vs. ==
  • Index(es):
    • Date
    • Thread