以下比较评估为真:
1)
@"foo" == @"foo";
2)
NSString *myString1 = @"foo";
NSString *myString2 = @"foo";
myString1 == myString2;
但是,肯定有时候使用等号运算符无法比较两个NSString
,而是需要[myString1 isEqualToString:myString2]
。有人可以对此有所了解吗?
答案 0 :(得分:161)
==
工作的原因是指针比较。使用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"];
答案 1 :(得分:13)
等于运算符==
仅比较指针地址。当您使用文字@""
语法创建两个相同的字符串时,编译器将检测它们是否相等,并且仅存储一次数据。因此,两个指针指向同一位置。但是,由其他方法创建的字符串可能包含相同的数据,但存储在不同的存储位置。因此,在比较字符串时,总是使用isEqual:
。
请注意,isEqual:
和isEqualToString:
始终返回相同的值,但isEqualToString:
更快。
答案 2 :(得分:10)
==
比较内存中的位置。 ptr == ptr2
如果它们都指向相同的内存位置。这恰好适用于字符串常量,因为编译器碰巧使用一个实际字符串来表示相同的字符串常量。如果你有相同内容的变量,将不会工作,因为它们会指向不同的内存位置;在这种情况下使用isEqualToString
。
答案 3 :(得分:6)
在Cocoa字符串中使用NSString的isEqualToString:
方法进行比较。
指针比较适用于您的情况,因为编译器足够温和,可以将两个字符串文字合并为一个对象。无法保证两个相同的字符串共享一个NSString
实例。
答案 4 :(得分:3)
一个示例,演示如何将地址比较作为字符串比较的代理进行破坏:
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];
答案 5 :(得分:0)
看看这个例子:
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和解引用指针,尽管它没有。正如你所见,指针是不同的。
答案 6 :(得分:-1)
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");
}