理解 NSString 比较
以下两个比较的结果均为 true:
1)
@"foo" == @"foo";
2)
NSString *myString1 = @"foo";
NSString *myString2 = @"foo";
myString1 == myString2;
但是,有时两个 NSString
无法使用相等运算符进行比较,并且 [myString1 isEqualToString:myString2]
是必需的。有人可以解释一下吗?
Both the following comparisons evaluate to true:
1)
@"foo" == @"foo";
2)
NSString *myString1 = @"foo";
NSString *myString2 = @"foo";
myString1 == myString2;
However, there are definitely times where two NSString
s cannot be compared using the equality operator, and [myString1 isEqualToString:myString2]
is required instead. Can someone shed some light on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
看看这个例子:
所以,编译器很可能使用 isEqualToString 方法来处理 NSString 的 isEquals 和取消引用指针,尽管它没有这样做。正如您所看到的,指针是不同的。
Check out this example:
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.
一个示例演示了地址比较作为字符串比较的替代项将如何被破坏:
An example demonstrating how address comparison as a surrogate for string comparison will break:
在 Cocoa 中,字符串是使用 NSString 的 isEqualToString: 方法进行比较的。
指针比较适用于您的情况,因为编译器足够温和,可以合并两个字符串文字以指向一个对象。无法保证两个相同的字符串共享一个
NSString
实例。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.==
比较内存中的位置。ptr == ptr2
如果它们都指向同一内存位置。这恰好适用于字符串常量,因为编译器恰好使用一个实际字符串来表示相同的字符串常量。如果您有具有相同内容的变量,则它不会工作,因为它们将指向不同的内存位置;在这种情况下使用 isEqualToString 。==
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; useisEqualToString
in such a case.相等运算符
==
仅比较指针地址。当您使用文字@""
语法创建两个相同的字符串时,编译器将检测它们是否相等,并且仅存储数据一次。因此,两个指针指向同一个位置。然而,通过其他方式创建的字符串可能包含相同的数据,但存储在不同的内存位置。因此,在比较字符串时,您应该始终使用isEqual:
。请注意,
isEqual:
和isEqualToString:
始终返回相同的值,但isEqualToString:
速度更快。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 useisEqual:
when comparing strings.Note that
isEqual:
andisEqualToString:
always return the same value, butisEqualToString:
is faster.==
起作用的原因是指针比较。当您使用@""
定义常量NSString
时,编译器会唯一化引用。当在代码中的其他位置定义相同的常量时,它们都将指向内存中相同的实际位置。比较
NSString
实例时,您应该使用isEqualToString:
方法:编辑:
initWithString:
不再创建新引用,您将需要initWithFormat
,The reason why
==
works is because of pointer comparison. When you define a constantNSString
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 theisEqualToString:
method:Edit:
initWithString:
does not create a new reference any more, you will needinitWithFormat
,