今天继续讲Objectitve-C的Block,讲Block作为属性,并讲使用copy修饰符来修饰Block属性的作用。
下面是ios reference官方的讲解:
Objects Use Properties to Keep Track of Blocks
The syntax to define a property to keep track of a block is similar to a block variable:
@interface XYZObject : NSObject
@property (copy) void (^blockProperty)(void);
@end
Note: 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. For more information, see Blocks Programming Topics.
A block property is set or invoked like any other block variable:
self.blockProperty = ^{
...
};
self.blockProperty();
It’s also possible to use type definitions for block property declarations, like this:
typedef void (^XYZSimpleBlock)(void);
@interface XYZObject : NSObject
@property (copy) XYZSimpleBlock blockProperty;
@end