简体   繁体   English

为什么NSNumber比较方法不同?

[英]Why NSNumber comparison method different?

I used NSNumber class. 我使用了NSNumber类。

but methods that can use is different in follow environments. 但是在后续环境中可以使用的方法有所不同。

First is a picture in command line tool environment. 首先是命令行工具环境中的图片。 (Mac os x) (Mac OS X)

Second is a picture in single view application environment. 其次是单视图应用程序环境中的图片。 (iOS) (IOS)

: Can't use isGreaterThan:, isGreaterThanOrEqualTo:, isLessThan:.. Etc. methods :不能使用isGreaterThan :, isGreaterThanOrEqualTo :, isLessThan:..等方法

Why did that? 为什么呢?

1] first picture 1]第一张图片

第一张图片

2] second picture 2]第二张图片

第二张图片

Those methods you're seeing on the Mac aren't actually part of the NSNumber class but instead are part of an informal protocol called NSComparisonMethods that is only available on Mac OS X. The methods in this protocol are convenience methods that actually just use the compare: method already available on many Cocoa classes. 您在Mac上看到的那些方法实际上并不是NSNumber类的一部分,而是属于NSComparisonMethods的非正式协议的一部分,该协议仅在Mac OS X上可用。此协议中的方法是方便的方法,实际上仅使用compare:许多可可类中已经提供的方法。 If you wanted, you could implement these methods as a category on NSNumber quite easily. 如果需要,可以很容易地将这些方法实现为NSNumber上的类别。 Here is how I would implement -isGreaterThanOrEqualTo: . 这是我实现-isGreaterThanOrEqualTo:

@interface NSNumber (ConvenientComparison)

- (BOOL)isGreaterThanOrEqualTo:(id)object;

@end

@implementation NSNumber (ConvenientComparison)

- (BOOL)isGreaterThanOrEqualTo:(id)object
{
    if (![object isKindOfClass:[NSNumber class]])
    {
        // Not sure what NSNumber does if you pass in something that isn't a number.
        // Handle this case
        return NO;
    }

    NSNumber *numberToCompareAgainst = (NSNumber *)object;

    NSComparisonResult comparisonResult = [self compare:numberToCompareAgainst];

    if (comparisonResult == NSOrderedSame || comparisonResult == NSOrdereedDescending)
        return YES;

    return NO;
}

Those methods are part of the NSComparisonMethods protocol, which exists on the Mac but not on iOS. 这些方法是NSComparisonMethods协议的一部分,该协议存在于Mac上,而不存在于iOS上。 As for why it doesn't exist on iOS: It's part of Cocoa's scripting support, which did not get ported to iOS since AppleScript and OSA don't exist there. 至于为什么它在iOS上不存在:这是Cocoa脚本支持的一部分,由于AppleScript和OSA在那里不存在,因此该脚本没有移植到iOS。

I am not sure of why there is a difference. 我不确定为什么会有区别。 I checked the documentation for ios 5.0 and OS X 10.6. 我检查了ios 5.0和OS X 10.6的文档。 They both don't have the isGreater... methods. 他们俩都没有isGreater ...方法。

You can compare your NSNumber s using 您可以使用比较您的NSNumber

- (NSComparisonResult)compare:(NSNumber *)aNumber

NSComparisonResult compResult = [test compare:aNumber];

and check for test >= aNumber 并检查测试> = aNumber

if (compResult == NSOrderedDescending || compResult == NSOrderedSame) {

}

Return Value NSOrderedAscending if the value of aNumber is greater than the receiver's, NSOrderedSame if they're equal, and NSOrderedDescending if the value of aNumber is less than the receiver's. 如果aNumber的值大于接收者的值,则返回值NSOrderedAscending;如果相等,则返回NSOrderedSame;如果aNumber的值小于接收者的值,则返回NSOrderedDescending。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM