Best Technique for Replacing Delegate Methods with Blocks

前端 未结 2 1868
忘掉有多难
忘掉有多难 2021-02-04 14:39

I\'m looking to create a category to replace delegate methods with callbacks blocks for a lot of the simple iOS APIs. Similar to the sendAsyc block on NSURLConnection. There are

2条回答
  •  生来不讨喜
    2021-02-04 14:55

    I had a similar problem and chose your option 2, but with the 2 small additions:

    1. Explicitly marking the delegate it implements like this:

      @interface __DelegateBlock:NSObject 
      
    2. Check to ensure the callback is not nil before calling:

      if (callbackBlock != nil) {
          callbackBlock(buttonIndex);
      }
      

提交回复
热议问题