开发者

NSOrderedDescending instead of NSOrderedSame for equal strings, why?

开发者 https://www.devze.com 2022-12-30 23:33 出处:网络
I\'m compairing two strings. There are equal. isEqualToString: returns NSOrderedDescending. I thought isEqualToString: would return NSOrderedSame. Can someone explain to me why this happens?

I'm compairing two strings. There are equal. isEqualToString: returns NSOrderedDescending.

I thought isEqualToString: would return NSOrderedSame. Can someone explain to me why this happens?

example case...

 NSString *myString = @"1";

 if ( [myString isEqualToString:@"1"] == NSOrderedSame ) {
    // is NSOrderedSame
 }

Since writing the question I know I should use instead compare:. It will return NSOrderedSame. But I w开发者_运维问答ould like to know, still, why this happens.

Thanks

Ross


It doesn't return NSOrderedDescending, it returns YES. Check the return type of that method!

- (BOOL)isEqualToString:(NSString *)aString

NSOrderedDescending is an NSComparisonResult, and happens to have a value of 1 due to its declaration:

enum {
   NSOrderedAscending = -1,
   NSOrderedSame,
   NSOrderedDescending
};
typedef NSInteger NSComparisonResult;

Don't mix and match types!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号