Thursday, November 8, 2012

Objective-C Identiy vs. Equality

// test for object identity:
BOOL identical = @"Peter" == @"Peter";     //YES
            identical = @"Peter" == @" Peter ";    //NO

// test for string equality: will return YES
NSString *aName = @"Peter";
NSMutableString *anotherName = [NSMutableString stringWithString:@"P"];

[anotherName appendString:@"eter"];

BOOL same = [aName isEqualToString:anotherName];     //YES
BOOL equal = (aName == anotherName);         //NO

// test for existence
BOOL exists = (object != nil);

No comments: