ios NSComparisonResult 和 NSComparator - 它们是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4118285/
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-08-30 18:02:35  来源:igfitidea点击:

NSComparisonResult and NSComparator - what are they?

iphoneobjective-ciosfunction-pointersobjective-c-blocks

提问by wh1t3cat1k

What is NSComparisonResultand NSComparator?

什么是NSComparisonResultNSComparator

I've seen one of the type definitions, something like that:

我见过一种类型定义,类似这样:

typedef NSComparisonResult (^NSComparator)(id obj1, id obj2);

Is it any different from a function pointer?

它与函数指针有什么不同吗?

Also, I can't even guess what the ^symbol means.

另外,我什至无法猜测^符号的含义。

回答by Jacob Relkin

^signifies a block type, similar in concept to a function pointer.

^表示块类型,在概念上类似于函数指针。

typedef NSComparisonResult (^NSComparator)(id obj1, id obj2);
//          ^                      ^                ^
//   return type of block      type name       arguments

This means that the type NSComparatoris a blockthat takes in two objects of type idcalled obj1and obj2, and returns an NSComparisonResult.

这意味着该类型NSComparator是一个,它接受两个id名为obj1and的类型的对象obj2,并返回一个NSComparisonResult

Specifically NSComparatoris defined in the Foundation Data Types reference.

具体NSComparatorFoundation Data Types 参考中定义。

And to learn more about C blocks, check out this ADC article Blocks Programming Topics.

要了解有关 C 模块的更多信息,请查看这篇 ADC 文章模块编程主题

Example:

例子:

NSComparator compareStuff = ^(id obj1, id obj2) {
   return NSOrderedSame;
};

NSComparisonResult compResult = compareStuff(someObject, someOtherObject);

回答by whitneyland

Jacob's answer is good, however to answer the part about "how is this different than a function pointer?":

雅各布的回答很好,但是要回答有关“这与函数指针有何不同?”的部分:

1) A block is nota function pointer. Blocks are Apple's take on how to make functions first class citizens in C/C++/Objective-C. It's new to iOS 4.0.

1) 块不是函数指针。块是 Apple 对如何在 C/C++/Objective-C 中使函数成为一等公民的看法。这是 iOS 4.0 的新功能。

2) Why introduce this strange concept? Turns out first class functions are useful in quite a few scenarios, for example managing chunks of work that can be executed in parallel, as in Grand Central Dispatch. Beyond GCD, the theory is important enough that there are entire software systems based around it. Lisp was one of the first.

2)为什么要引入这个奇怪的概念?事实证明,一流的功能在很多场景中都很有用,例如管理可以并行执行的工作块,如 Grand Central Dispatch。除了 GCD 之外,该理论非常重要,以至于整个软件系统都基于它。Lisp 是第一个。

3) You will see this concept in many other languages, but by different names. For example Microsoft .Net has lambdas and delegates (no relation to Objective-C delegates), while the most generic names are probably anonymous functions or first class functions.

3) 您会在许多其他语言中看到这个概念,但名称不同。例如,Microsoft .Net 有 lambdas 和委托(与 Objective-C 委托无关),而最通用的名称可能是匿名函数或第一类函数

回答by Aruna

NSComparisonResult comparisionresult;
NSString * alphabet1;
NSString * alphabet2;


// Case 1

alphabet1 = @"a";
alphabet2 = @"A";
comparisionresult = [alphabet1 caseInsensitiveCompare:alphabet2];

if (comparisionresult == NSOrderedSame)
    NSLog(@"a and a are same. And the NSComparisionResult Value is %ld \n\n", comparisionresult);
//Result: a and a are same. And the NSComparisionResult Value is 0

// Case 2
alphabet1 = @"a";
alphabet2 = @"B";
comparisionresult = [alphabet1 caseInsensitiveCompare:alphabet2];

if (comparisionresult == NSOrderedAscending)
    NSLog(@"a is greater than b. And the NSComparisionResult Value is %ld \n\n", comparisionresult);
//Result: a is greater than b. And the NSComparisionResult Value is -1

// Case 3
alphabet1 = @"B";
alphabet2 = @"a";
comparisionresult = [alphabet1 caseInsensitiveCompare:alphabet2];

if (comparisionresult == NSOrderedDescending)
    NSLog(@"b is less than a. And the NSComparisionResult Value is %ld", comparisionresult);

//Result: b is less than a. And the NSComparisionResult Value is 1