我目前有一个 web 服务器,用于与我的 iPhone 应用程序通过 SOAP 进行通信。我返回一个包含 GUID 的 NSString
,但当我尝试将其与另一个 NSString
进行比较时,结果很奇怪。
为什么这不会触发呢?这两个字符串肯定是匹配的吧?
NSString *myString = @"hello world";
if (myString == @"hello world")
return;
我目前有一个 web 服务器,用于与我的 iPhone 应用程序通过 SOAP 进行通信。我返回一个包含 GUID 的 NSString
,但当我尝试将其与另一个 NSString
进行比较时,结果很奇怪。
为什么这不会触发呢?这两个字符串肯定是匹配的吧?
NSString *myString = @"hello world";
if (myString == @"hello world")
return;
使用-isEqualToString:
方法比较两个字符串的值。使用C的 ==
操作符将只比较对象的地址。
if ([category isEqualToString:@"Some String"])
{
// Do stuff...
}
你可以根据需要使用大小写敏感或不敏感的比较方式。大小写敏感的比较方式如下:
if ([category isEqualToString:@"Some String"])
{
// Both strings are equal without respect to their case.
}
大小写不敏感可以这么做:
if ([category compare:@"Some String" options:NSCaseInsensitiveSearch] == NSOrderedSame)
{
// Both strings are equal with respect to their case.
}
NSString *first = @"abc";
NSString *second = @"abc";
NSString *third = [[NSString alloc] initWithString:@"abc"];
NSLog(@"%d", (second == third))
NSLog(@"%d", (first == second));
NSLog(@"%d", [first isEqualToString:second]);
NSLog(@"%d", [first isEqualToString:third]);
Output will be :-
0
1
1
1