Search code examples
objective-cfunction-parameter

Objective-C method parameter with ^ symbol


One of the method signature in objective-c code

-(void)funcName: (const NSString *)name parameter: (void(^)(ClassName *input)) obj

The class definition header file is

@interface ClassName : NSObject
@property NSObject *data;
@end

Now how to prepare and pass the second paramater?


Solution

  • you can pass a block as a parameter for instance like:

    option 1.

    [... funcName:@"" parameter:^(ClassName * input) {
        NSLog(@"I'm inside the block!");
    }];
    

    option 2.

    void(^myBlock)(ClassName *) = ^(ClassName * input) {
        NSLog(@"I'm inside the block!");
    };
    
    [... funcName:@"" parameter:myBlock];
    

    both options can work, you can use whichever makes more sense to you.