理解 NSString 比较

以下两项比较的结果均为真:

1)

@"foo" == @"foo";

2)

NSString *myString1 = @"foo";
NSString *myString2 = @"foo";
myString1 == myString2;

但是,有时候确实不能使用相等运算符比较两个 NSString,而需要使用 [myString1 isEqualToString:myString2]。有人能解释一下吗?

103227 次浏览

In Cocoa strings are compared using NSString's isEqualToString: method.

Pointer comparison works in your case because the compiler is gentle enough to merge the two string literals to point to one object. There's no guarantee that two identical strings share one NSString instance.

The reason why == works is because of pointer comparison. When you define a constant NSString using @"", the compiler uniquifies the reference. When the same constants are defined in other places in your code, they will all point to the same actual location in memory.

When comparing NSString instances, you should use the isEqualToString: method:

NSString *myString1 = @"foo";
NSString *myString2 = @"foo";
NSString *myString3 = [[NSString alloc] initWithString:@"foo"];
NSLog(@"%d", (myString2 == myString3))  //0
NSLog(@"%d", (myString1 == myString2)); //1
NSLog(@"%d", [myString1 isEqualToString:myString2]); //1
NSLog(@"%d", [myString1 isEqualToString:myString3]); //1
[myString3 release];

Edit:

NSString *myString3 = [[NSString alloc] initWithString:@"foo"];
// this is same with @"foo"

initWithString: does not create a new reference any more, you will need initWithFormat,

NSString *myString3 = [[NSString alloc] initWithFormat:@"foo"];

The equality operator == only compares pointer addresses. When you create two identical strings using the literal @"" syntax, the compiler will detect that they are equal, and only store the data once. Hence, the two pointers point to the same location. However, strings created by other means may contain identical data, yet be stored at different memory locations. Hence, you should always use isEqual: when comparing strings.

Note that isEqual: and isEqualToString: always return the same value, but isEqualToString: is faster.

== compares locations in memory. ptr == ptr2 if they both point to the same memory location. This happens to work with string constants because the compiler happens to use one actual string for identical string constants. It won't work if you have variables with the same contents, because they'll point to different memory locations; use isEqualToString in such a case.

An example demonstrating how address comparison as a surrogate for string comparison will break:

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];


NSString *s1 = @"foo";
NSString *s2 = @"foo";
NSString *s3 = [[[NSString alloc] initWithString:@"foo"] autorelease];
NSMutableString *s4 = [NSMutableString stringWithString:@"foobar"];
[s4 replaceOccurrencesOfString:@"bar"
withString:@""
options:NSLiteralSearch
range:NSMakeRange(0, [s4 length])];


NSLog(@"s1 = %p\n", s1);
NSLog(@"s2 = %p\n", s2);
NSLog(@"s3 = %p\n", s3);
NSLog(@"s4 = %p\n", s4); // distinct from s1


NSLog(@"%i", [s1 isEqualToString:s4]); // 1


[pool release];

Check out this example:

NSString *myString1 = @"foo";
NSMutableString *myString2 = [[NSMutableString stringWithString:@"fo"] stringByAppendingString: @"o"];


NSLog(@"isEquality: %@", ([myString1 isEqual:myString2]?@"+":@"-")); //YES
NSLog(@"isEqualToStringity: %@", ([myString1 isEqualToString:myString2]?@"+":@"-")); //YES
NSLog(@"==ity: %@", ((myString1 == myString2)?@"+":@"-")); // NO

So, the compiler is likely to use isEqualToString method to process isEquals for NSString's and dereference pointers, though it had not to. And the pointers are different, as you see.

  NSString *str1=[NSString stringWithFormat:@"hello1"];
NSString *str2=[NSString stringWithFormat:@"hello1"];
NSString *str3 = [[NSString alloc] initWithString:@"hello1"];








// == compares the pointer but in our example we are taking same string value to different object  using @  so it will point to same address so output will be TRUE condition
if (str1==str2) {
NSLog(@"Both String are equal");
}
else{
NSLog(@"Both String not are equal");
}




// == compares the pointer but in our example we are taking same string value to different object but we have allocated different string so both object will pount to different address so output will be FALSE condition
if (str1==str3) {


NSLog(@"Both String are equal");
}
else{
NSLog(@"Both String not are equal");


}




// compare:= compares the values of objects so output will be TRUE condition
if ([str1 compare:str3]== NSOrderedSame) {
NSLog(@"Both String are equal");


}
else{
NSLog(@"Both String not are equal");


}




// isEqual compares the values of objects so output will be TRUE condition


if ([str1 isEqual:str2]) {


NSLog(@"Both String are equal");
}
else{
NSLog(@"Both String not are equal");


}


// isEqual compares the values of objects so output will be TRUE condition


if ([str1 isEqual:str3]) {


NSLog(@"Both String are equal");
}
else{
NSLog(@"Both String not are equal");


}




// isEqualToString compares the values of objects so output will be TRUE condition
if ([str1 isEqualToString:str2]) {


NSLog(@"Both String are equal");
}
else{
NSLog(@"Both String not are equal");


}




// isEqualToString compares the values of objects so output will be TRUE condition
if ([str1 isEqualToString:str3]) {


NSLog(@"Both String are equal");
}
else{
NSLog(@"Both String not are equal");


}


// == compares the pointers since we have initialized the same value to first object so the pointer be be same for same value so output will be TRUE condition
if (str1==@"hello1") {


NSLog(@"Both String are equal");
}
else{
NSLog(@"Both String not are equal");


}