How to perform Callbacks in Objective-C

后端 未结 5 1328
无人及你
无人及你 2020-11-27 09:09

How to perform call back functions in Objective-C?

I would just like to see some completed examples and I should understand it.

5条回答
  •  情深已故
    2020-11-27 09:45

    To keep this question up-to-date, iOS 5.0's introduction of ARC means this can be achieved using Blocks even more concisely:

    @interface Robot: NSObject
    + (void)sayHi:(void(^)(NSString *))callback;
    @end
    
    @implementation Robot
    + (void)sayHi:(void(^)(NSString *))callback {
        // Return a message to the callback
        callback(@"Hello to you too!");
    }
    @end
    
    [Robot sayHi:^(NSString *reply){
      NSLog(@"%@", reply);
    }];
    

    There's always F****ng Block Syntax if you ever forget Objective-C's Block syntax.

提交回复
热议问题