目标C-使用ARC的自定义设置器? [英] Objective C - Custom setter with ARC?

查看:62
本文介绍了目标C-使用ARC的自定义设置器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我以前用来编写自定义保留设置器的方式:

Here is how I used to write a custom retained setter before:

- (void)setMyObject:(MyObject *)anObject
{
   [_myObject release], _myObject =  nil;
   _myObject = [anObject retain];

   // Other stuff
}

当属性设置为Strong时,如何使用ARC实现此目的. 如何确保变量具有强指针?

How can I achieve this with ARC when the property is set to strong. How can I make sure that the variable has strong pointer?

推荐答案

strong在ivar级别上照顾自己,因此您只能这样做

The strong takes care of itself on the ivar level, so you can merely do

- (void)setMyObject:(MyObject *)anObject
{
   _myObject = anObject;
   // other stuff
}

就是这样.

注意:如果您在没有自动属性的情况下执行此操作,则ivar将为

Note: if you're doing this without automatic properties, the ivar would be

MyObject *_myObject;

,然后ARC(非常感谢)为您处理保留和释放. __strong是默认的限定词.

and then ARC takes cares of the retains and releases for you (thankfully). __strong is the qualifier by default.

这篇关于目标C-使用ARC的自定义设置器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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