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 protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to