了解NSString比较

以下比较评估为真:

1)

@"foo" == @"foo"; 

2)

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

然而,当然有两个NSString不能使用相等运算符进行比较,而[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]; 

等号运算符==只比较指针地址。 当使用文字@""语法创建两个相同的字符串时,编译器将检测到它们是相等的,只存储一次数据。 因此,这两个指针指向相同的位置。 但是,通过其他方式创建的字符串可能包含相同的数据,但可能存储在不同的存储位置。 因此,在比较字符串时,应始终使用isEqual:

注意isEqual:isEqualToString:总是返回相同的值,但是isEqualToString:更快。

==比较内存中的位置。 ptr == ptr2如果他们都指向相同的内存位置。 这恰好与字符串常量一起工作,因为编译器恰好将一个实际字符串用于相同的字符串常量。 如果你有相同的内容变量, 它将不会工作,因为他们会指向不同的内存位置; 在这种情况下使用isEqualToString

在可可字符串中使用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方法来处理isEquals的NSString和取消引用指针,尽管它没有。 如你所见,指针是不同的。

  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"); }