在ARC下,通过属性分配给ivar时,是否会自动复制块? [英] Under ARC, are Blocks automatically copied when assigned to an ivar via the property?

查看:81
本文介绍了在ARC下,通过属性分配给ivar时,是否会自动复制块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设

typedef void (^MyResponseHandler) (NSError *error);
@property (strong, nonatomic) MyResponseHandler ivarResponseHandler;
synthesize ivarResponseHandler = _ivarResponseHandler;

- (void)myMethod:(MyResponseHandler)responseHandler
{
    self.ivarResponseHandler = responseHandler;
    ...
}

通过@property分配给ivar是否正确?我知道在手动内存管理中,您需要self.ivarResponseHandler = [responseHandler copy];来确保将块从堆栈复制到堆.但是,在观看WWDC 2011的第322节-深度上的Objective-C进步(第25分钟)时,这位演讲者说ARC自动处理了将块分配给ivar的问题.我只是想确定一下.

Is the assignment to the ivar through the @property correct? I know that in manual memory management you would have needed self.ivarResponseHandler = [responseHandler copy]; to make sure the block was copied from the stack to the heap. But watching Session 322 - Objective-C Advancements in Depth (minute 25) from WWDC 2011, the speaker says that ARC automatically handles the assignment of a block to an ivar. I just wanted to make sure.

推荐答案

bearMountain,

bearMountain,

实际上,确保复制块的最简单方法是将@property设置为使用复制.如:

The easiest way to ensure the block is copied is to, in fact, set the @property to use copy. As in:

@property (copy, nonatomic) MyResponseHandler ivarResponseHandler;

它可以满足您的所有需求.

It does everything you need.

安德鲁

这篇关于在ARC下,通过属性分配给ivar时,是否会自动复制块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆