在Objective C中为弱属性创建自定义设置器的正确方法是什么? [英] What is the correct way to create a custom setter for a weak property in Objective C?

查看:95
本文介绍了在Objective C中为弱属性创建自定义设置器的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我声明这样的属性:

If I declare a property like this:

@property(nonatomic,weak) Foo *someProperty;

然后我像这样声明一个自定义的setter:

and I then declare a custom setter like so:

- (void)setSomeProperty:(Foo *)someProp {
    _someProperty = someProp;
    //...more custom stuff
}

这有什么问题吗?也就是说,编译器应使用__weak修饰符自动合成_someProperty ivar,因此仅在上面的setter中进行分配就足够了,对吗?

is there anything wrong with this? That is, the compiler should automatically synthesize the _someProperty ivar with the __weak modifier, so just doing the assignment in the setter above should suffice, correct?

推荐答案

是的,仅此而已.当然,如果您想要除_someProperty之外的其他内容,则可以指定自定义iVar:

Yes, that's all there is to it. Of course you can specify a custom iVar if you'd like something other than _someProperty like:

@synthesize someProperty = someProperty_;

这篇关于在Objective C中为弱属性创建自定义设置器的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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