如何在Objective C中宣告Block?

weixin_33797791發表於2017-12-13

本文翻譯自http://fuckingblocksyntax.com/

作為區域性變數:

  returnType(^blockName)(parameterTypes) = ^returnType(parameters) {...};
複製程式碼

作為屬性:

  @property (nonatomic, copy) returnType (^blockName)(parameterTypes);
複製程式碼

作為引數:

  - (void)someMethodThatTakesABlock:(returnType (^nullability)(parameterTypes))blockName;
複製程式碼

作為方法呼叫引數:

  [someObject someMethodThatTakesABlock:^returnType (parameters) {...} ];
複製程式碼

作為typedef

  typedef returnType (^TypeName)(parameterTypes);
  TypeName blockName = ^returnType(parameters) {...};
複製程式碼

相關文章