如何创建返回块的objective-c方法
让我们假设它还没有工作。像这样返回块的方法签名应该是如何创建返回块的objective-c方法,objective-c,block,objective-c-blocks,Objective C,Block,Objective C Blocks,让我们假设它还没有工作。像这样返回块的方法签名应该是 + (NSComparator)(^)(id obj1, id obj2) { (NSComparator)(^ block)(id obj1, id obj2) = {...} return block; } 这将分解为: +(NSInteger (^)(id, id))comparitorBlock { .... } 更新:在您的特定情况下,NSComparator已经是块类型。其定义是: +(NSIntege
+ (NSComparator)(^)(id obj1, id obj2)
{
(NSComparator)(^ block)(id obj1, id obj2) = {...}
return block;
}
这将分解为:
+(NSInteger (^)(id, id))comparitorBlock {
....
}
更新:在您的特定情况下,NSComparator
已经是块类型。其定义是:
+(NSInteger (^)(id, id))comparitorBlock;
^^ ^ ^ ^ ^ ^ ^
ab c d e e b f
a = Static Method
b = Return type parenthesis for the method[just like +(void)...]
c = Return type of the block
d = Indicates this is a block (no need for block names, it's just a type, not an instance)
e = Set of parameters, again no names needed
f = Name of method to call to obtain said block
因此,您只需返回以下typedef:
typedef NSComparisonResult (^NSComparator)(id obj1, id obj2);
你说的“它不起作用”到底是什么意思?这对于正确的错误描述来说太宽泛了。不<代码>比较程序已经是块类型。您可能指的是
+(NSComparisonResult(^)(id,id))比较器
或+(nscompariator)比较器
。哎呀,疏忽了!谢谢是的,至少对于这个问题,我会选择两个比较器中的+(NSComparator)comparator
。选定和+1。谢谢。我只是不想选择那些仍然是错误的答案,因为大多数人都不阅读CommonBTS。这会让其他读者感到困惑:)
typedef NSComparisonResult (^NSComparator)(id obj1, id obj2);
+ (NSComparator)comparator {
....
}