使用ARC为什么要再使用@properties? [英] With ARC why use @properties anymore?

查看:87
本文介绍了使用ARC为什么要再使用@properties?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在非ARC代码中,保留属性使用 self.property = 语法为您轻松处理内存管理,因此我们被教导几乎可以使用它们。

In non-ARC code retained properties handily take care of memory management for you using the self.property = syntax, so we were taught to use them for practically everything.

但现在使用ARC,这种内存管理不再是问题,使用属性的原因是否会消失?还有什么好的理由(显然除了提供对实例变量的公共访问权限之外)才能再使用属性了吗?

But now with ARC this memory management is no longer an issue, so does the reason for using properties evaporate? is there still any good reason (obviously other than providing public access to instance variables) to use properties anymore?

推荐答案


但是现在使用ARC这个内存管理不再是问题,因此使用属性的原因会消耗掉吗?是否还有任何好的
原因(显然除了提供实例
变量的公共访问权限)以便再使用属性了?

But now with ARC this memory management is no longer an issue, so does the reason for using properties evaporate? is there still any good reason (obviously other than providing public access to instance variables) to use properties anymore?

是 - 通过使用@property和@synthesized getters / setter,您保证:

Yes -- by using @property and @synthesized getters/setters, you guarantee that:


  • 您的getter / setter可以是子类和子类可以覆盖存储和/或行为

  • 一切都保持良好的封装

  • 你可以使用观察钩子 - KVO等... - 内部和外部监控变更

  • 您可以方便地在读取和/或写入属性时设置断点

  • 如果内部只有实例变量需要暴露,这是复制@property声明本身的问题;更少的重构。

  • 你可以利用所有各种修饰符关键字的声明能力 - 复制,强,弱,原子等... - 编译器正在采取的越来越多过度优势的好处

  • your getter/setter can be subclassed and subclasses can override storage and/or behavior
  • everything remains nicely encapsulated
  • you can use observation hooks -- KVO, etc... -- to monitor changes internally and externally
  • you have a convenient spot to set a breakpoint on read and/or write to the property
  • if that "internal only" instance variable were need to be exposed, it is a matter of copying the @property declaration itself; much less refactoring.
  • you can leverage the declarative power of all the various modifier keywords -- copy, strong, weak, atomic, etc.. -- which the compiler is taking more and more advantage of over thime

即使在课堂内部,我也倾向于使用属性和点语法来维护对象内的状态。这并非普遍正确 - 我会直接操作一些实例变量(完全直接操作;根本没有@property)如果我的设计是暴露意味着无论如何都会进行大规模的重构。

Even internally to a class, I generally lean to using properties and dot syntax to maintain state within the object. That isn't universally true -- there will be some instance variables that I directly manipulate (exclusively directly manipulate; no @property at all) if my design is such that exposure would imply a massive refactoring anyway.

这篇关于使用ARC为什么要再使用@properties?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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