NSComparisonResult and NSComparator - what are they?

前端 未结 3 1051
隐瞒了意图╮
隐瞒了意图╮ 2021-02-09 06:09

What is NSComparisonResult and NSComparator?

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

typedef NSComparisonR         


        
3条回答
  •  难免孤独
    2021-02-09 06:32

    ^ 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 NSComparator is a block that takes in two objects of type id called obj1 and obj2, and returns an NSComparisonResult.

    Specifically NSComparator is defined in the Foundation Data Types reference.

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

    Example:

    NSComparator compareStuff = ^(id obj1, id obj2) {
       return NSOrderedSame;
    };
    
    NSComparisonResult compResult = compareStuff(someObject, someOtherObject);
    

提交回复
热议问题