以下两个比较都评估为真:
1)
@"foo" == @"foo";
2)
NSString *myString1 = @"foo";
NSString *myString2 = @"foo";
myString1 == myString2;
但是,肯定有两个NSString
s 不能使用相等运算符比较的时候,[myString1 isEqualToString:myString2]
而是需要的。有人可以对此有所了解吗?
以下两个比较都评估为真:
1)
@"foo" == @"foo";
2)
NSString *myString1 = @"foo";
NSString *myString2 = @"foo";
myString1 == myString2;
但是,肯定有两个NSString
s 不能使用相等运算符比较的时候,[myString1 isEqualToString:myString2]
而是需要的。有人可以对此有所了解吗?
之所以==
有效是因为指针比较。当您使用 定义常量NSString
时@""
,编译器会统一引用。当在代码的其他位置定义相同的常量时,它们都将指向内存中的相同实际位置。
在比较NSString
实例时,您应该使用以下isEqualToString:
方法:
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];
编辑:
NSString *myString3 = [[NSString alloc] initWithString:@"foo"];
// this is same with @"foo"
initWithString:
不再创建新引用,您将需要initWithFormat
,
NSString *myString3 = [[NSString alloc] initWithFormat:@"foo"];
相等运算符==
仅比较指针地址。当您使用文字@""
语法创建两个相同的字符串时,编译器将检测它们是否相等,并且只存储一次数据。因此,这两个指针指向同一个位置。但是,通过其他方式创建的字符串可能包含相同的数据,但存储在不同的内存位置。因此,在比较字符串时应始终使用。isEqual:
请注意,isEqual:
andisEqualToString:
总是返回相同的值,但isEqualToString:
速度更快。
==
比较内存中的位置。ptr == ptr2
如果它们都指向相同的内存位置。这恰好适用于字符串常量,因为编译器碰巧将一个实际字符串用于相同的字符串常量。如果您有具有相同内容的变量,它将不起作用,因为它们将指向不同的内存位置;isEqualToString
在这种情况下使用。
在 Cocoa 中,字符串是使用 NSString 的isEqualToString:
方法进行比较的。
指针比较适用于您的情况,因为编译器足够温和,可以合并两个字符串文字以指向一个对象。不能保证两个相同的字符串共享一个NSString
实例。
一个示例演示地址比较作为字符串比较的代理将如何中断:
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];
看看这个例子:
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
因此,编译器可能会使用 isEqualToString 方法来处理 NSString 和取消引用指针的 isEquals,尽管它没有这样做。如您所见,指针是不同的。
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");
}