Created
May 12, 2013 00:05
-
-
Save emarashliev/5561872 to your computer and use it in GitHub Desktop.
Explanation isEqual: and == comparison for the Objective-C mutable and immutable objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Set non ARC flag (-fno-objc-arc) for the execution file | |
// http://stackoverflow.com/questions/6646052/how-can-i-disable-arc-for-a-single-file-in-a-project | |
// | |
// | |
NSMutableArray *mutableArrayOrginal = [NSMutableArray arrayWithObjects:@"asdasd", nil]; | |
NSLog(@"mutableArrayOrginal address is: %p", mutableArrayOrginal); | |
NSMutableArray *mutableArrayCopy = [mutableArrayOrginal copy]; | |
NSLog(@"mutableArrayCopy address is: %p", mutableArrayCopy); | |
if ([mutableArrayOrginal isEqual:mutableArrayCopy]) { | |
NSLog(@"Mutable Arrays Equal (isEqual:)"); | |
} else { | |
NSLog(@"Mutable Arrays NOT Equal (isEqual:)"); | |
} | |
if (mutableArrayOrginal == mutableArrayCopy) { | |
NSLog(@"Mutable Arrays Equal (==)"); | |
} else { | |
NSLog(@"Mutable Arrays NOT Equal (==)"); | |
} | |
NSArray *immutableOrginal = [NSArray arrayWithObjects:@"asdasd", nil]; | |
NSLog(@"immutableOrginal address is: %p", immutableOrginal); | |
NSLog(@"immutableOrginal retain count, before copy is: %i", [immutableOrginal retainCount]); | |
NSArray *immutableCopy = [immutableOrginal copy]; | |
NSLog(@"immutableOrginal retain count, after copy is: %i", [immutableOrginal retainCount]); | |
NSLog(@"immutableCopy address is: %p", immutableCopy); | |
if ([immutableOrginal isEqual:immutableCopy]) { | |
NSLog(@"Immutable Arrays Equal (isEqual:)"); | |
} else { | |
NSLog(@"Immutable Arrays NOT Equal (isEqual:)"); | |
} | |
if (immutableOrginal == immutableCopy) { | |
NSLog(@"Immutable Arrays Equal (==)"); | |
} else { | |
NSLog(@"Immutable Arrays NOT Equal (==)"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment