使用弱与只读属性? [英] Using weak with readonly property?

查看:79
本文介绍了使用弱与只读属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该使用一次

@属性(非原子,弱,只读)

@属性(非原子,只读)

Weak的优点是,如果实例被释放,则将实例清零,但是只读是否意味着弱?如果一个显式属性想要弱属性,是否应该将其声明为弱属性?

Weak has the advantage of nil-ing out the instance if it gets deallocated, but is readonly implying weak? Should one explicit declare a property as weak if it want the weak behaviour?

推荐答案

如果要保留指向对象的指针您不拥有但希望它仅在存在的情况下才有效,那么您想使用一个弱指针,因为当所有者将其释放时,您的指针将自动设置为nil并且不会指向

If you want to keep a pointer to an object that you don't own but want it to be valid only as long as it exists, then you want to use a weak pointer because when it gets deallocated by the owner, your pointer will automatically get set to nil and won't be pointing to memory that it shouldn't be.

这两个都有不同的含义,如果只读,则不会有任何区别

These both have differnect meaning, readonly doesn't make any differnce if it is weak or strong.

@property (nonatomic, weak, readonly)
@property (nonatomic, readonly)

您还可以找到一些

You can also find some reference here.

这篇关于使用弱与只读属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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