objective-c 比较 Cocoa 中的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/881335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:34:02  来源:igfitidea点击:

Comparing Strings in Cocoa

objective-ccocoansstringcompare

提问by mattdwen

I have tried:

我试过了:

- (NSString*) generateString
{
    NSString* stringToReturn = @"thisString";
    return stringToReturn;
}

- (void) otherMethod
{
    NSString *returnedString = [self generateString];
    if (returnedString == @"thisString")
    { // Do this }
    else if (returnedString == @"thatString")
    { // Do that }
}

Which never matches.

从不匹配。

I have then tried

然后我试过了

if ([returnedString compare:@"thisString"] == 1)

But the compare method always returns 1 for me, even when comparing with a different string.

但是 compare 方法总是为我返回 1,即使在与不同的字符串进行比较时也是如此。

What is the correct way to do this, and what result should I expect?

这样做的正确方法是什么,我应该期待什么结果?

回答by newacct

First of all, you are using the ==operator to compare two object pointers (of type NSString *). So that returns true when the pointers are the same, not when the strings have the same contents. If you wanted to compare whether two strings are the same, you should use isEqualToString:or isEqual:(isEqual:is more general as it works for all types of objects).

首先,您使用==运算符来比较两个对象指针(类型为NSString *)。所以当指针相同时返回 true,而不是当字符串具有相同的内容时。如果你想比较两个字符串是否相同,你应该使用isEqualToString:or isEqual:(isEqual:更通用,因为它适用于所有类型的对象)。

Second, compare:returns 0(NSOrderSame) when they are the same, and 1(NSOrderedDescending) when the first is greater than the second. So in fact it returns 1 onlywhen they are different (specifically, when the first is greater than the second).

其次,compare:返回0NSOrderSame)当它们是相同的,和1NSOrderedDescending)当所述第一比第二大。所以实际上它只有在它们不同时才返回 1 (特别是当第一个大于第二个时)。

回答by ibz

[returnedString isEqualToString: @"thisString"]

回答by Gordon Wilson

When comparing two identical strings comparewill return NSOrderedSame, which is 0. It can also return NSOrderedAscending, -1, and NSOrderedDescending, 1.

比较两个相同的字符串时compare会返回NSOrderedSame,即 0。它也可以返回NSOrderedAscending、-1 和NSOrderedDescending1。

You may prefer to use isEqualToStringwhich returns YES or NO.

您可能更喜欢使用isEqualToStringwhich 返回 YES 或 NO。

回答by chaimp

if ([returnedString isEqualToString:@"thisString"])
    NSLog(@"Equal");
else
    NSLog(@"Not Equal");

回答by mouviciel

The == operator when applied to objects check whether the pointers are equal.

== 运算符应用于对象时检查指针是否相等。

You have to use -comparemethod or one of its companions -compare:options:-compare:options:range:or -compare:options:range:locale:which return a NSComparisonResult(NSOrderAscending, NSOrderSame, NSOrderDescending)

您必须使用-compare方法或其同伴之一compare:options:-compare:options:range:-compare:options:range:locale:返回NSComparisonResult( NSOrderAscending, NSOrderSame, NSOrderDescending)

If you just need equality comparison, you can use -isEqualToString:which returns a BOOLvalue.

如果您只需要相等比较,您可以使用-isEqualToString:which 返回一个BOOL值。