在 Objective-C 中如何定义 Block | Block in ObjC


Declaration

  • As a local variable:
returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};
  • As a property:
@property (nonatomic, copy, nullability) returnType (^blockName)(parameterTypes);
  • As a method parameter:
- (void)someMethodThatTakesABlock:(returnType (^nullability)(parameterTypes))blockName;
  • As an argument to a method call:
[someObject someMethodThatTakesABlock:^returnType (parameters) {...}];
  • As a typedef:
typedef returnType (^TypeName)(parameterTypes);
TypeName blockName = ^returnType(parameters) {...};

Hint

It is still a good idea to declare block properties as having copy semantics since a block assigned to a strong property will in fact be copied.

Apple recommends this as well:

You should specify copy as the property attribute, because a block needs to be copied to keep track of its captured state outside of the original scope. This isn’t something you need to worry about when using Automatic Reference Counting, as it will happen automatically, but it’s best practice for the property attribute to show the resultant behavior.