Suppose I have a NSString* str1 and NSMutableString* str2, and I make str2 the mutable copy of str1. And I called the following method:
-(void)someMethod {
NSString *str1;
NSMutableString *str2;
str1 = @"OK";
str2 = [str1 mutableCopy];
if ([str2 isEqual:str1]) {
NSLog(@"Same!");
}
else {
NSLog(@"Not exactly!");
}
NSLog(@"%@", [[str1 class] description]);
NSLog(@"%@", [[str2 class] description]);
}
Console output:
2014-01-07 14:03:16.291 LearnFoundation[3739:303] Same!
2014-01-07 14:03:16.293 LearnFoundation[3739:303] __NSCFConstantString
2014-01-07 14:03:16.293 LearnFoundation[3739:303] __NSCFString
So here comes the confusion, according to the documentation of isEqual
in NSString
, it returns a Boolean value that indicates whether the receiver and a given object are equal
. So why the mutable copy is said to be the same as the original immutable one?
Thanks in advance!