How do I create an objective-c method that return a block

前端 未结 1 1655
滥情空心
滥情空心 2020-12-09 09:12
-(NSMutableArray *)sortArrayByProminent:(NSArray *)arrayObject
{
    NSArray * array = [arrayObject sortedArrayUsingComparator:^(id obj1, id obj2) {
        Business         


        
相关标签:
1条回答
  • 2020-12-09 10:00

    A method signature to return a block like this should be

    +(NSInteger (^)(id, id))comparitorBlock {
        ....
    }
    

    This decomposes to:

    +(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
    

    Update: In your particular situation, NSComparator is already of a block type. Its definition is:

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

    As such, all you need to do is return this typedef:

    + (NSComparator)comparator {
       ....
    }
    
    0 讨论(0)
提交回复
热议问题