在ARC下面假设以下代码,
typedef void (^MyResponseHandler) (NSError *error);
@interface MyClass : NSObject
{
MyResponseHandler _ivarResponseHandler;
}
- (void)myMethod:(MyResponseHandler)responseHandler
{
_ivarResponseHandler = responseHandler;
...
}
Run Code Online (Sandbox Code Playgroud)
问题:当分配给ivar时,块是否自动复制到堆中?
我之前的问题暗示它是在通过a分配时复制的@property.但是,今天我使用了上面的代码并收到了EXC_BAD_ACCESS修改后修改的代码
_ivarResponseHandler = [responseHandler copy].
假设
typedef void (^MyResponseHandler) (NSError *error);
@property (strong, nonatomic) MyResponseHandler ivarResponseHandler;
synthesize ivarResponseHandler = _ivarResponseHandler;
- (void)myMethod:(MyResponseHandler)responseHandler
{
self.ivarResponseHandler = responseHandler;
...
}
Run Code Online (Sandbox Code Playgroud)
通过@property正确的方式分配给ivar 吗?我知道在手动内存管理中你需要self.ivarResponseHandler = [responseHandler copy];确保将块从堆栈复制到堆中.但是,观看会议322 - 来自WWDC 2011的会议-C深度进展(第25分钟),发言人说ARC自动处理将块分配给ivar.我只是想确定一下.
properties objective-c objective-c-blocks automatic-ref-counting
给出以下(手动引用计数):
void (^block)(void) = ^ {
NSLog(@"wuttup");
}
void (^async_block)(void) = ^ {
block();
}
dispatch_async(dispatch_get_main_queue(), async_block);
Run Code Online (Sandbox Code Playgroud)
将"阻止"复制而不是从堆栈中抛出并销毁?