问题描述
什么是 NSComparisonResult
和 NSComparator
?
我见过其中一个类型定义,类似于:
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.
推荐答案
^
表示块类型,在概念上类似于函数指针。
^
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
这意味着类型 NSComparator
是一个块,它接收两个类型为 id
调用 obj1
和 obj2
,并返回 NSComparisonResult
。
This means that the type NSComparator
is a block that takes in two objects of type id
called obj1
and obj2
, and returns an NSComparisonResult
.
具体 NSComparator
在。
要了解有关C块的更多信息,请查看此ADC文章。
And to learn more about C blocks, check out this ADC article Blocks Programming Topics.
示例:
NSComparator compareStuff = ^(id obj1, id obj2) {
return NSOrderedSame;
};
NSComparisonResult compResult = compareStuff(someObject, someOtherObject);
这篇关于NSComparisonResult和NSComparator - 它们是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!