Obj-C,一切的属性
我已经开始在一家新公司工作,我的团队领导告诉我要遵守的准则之一是很少使用保留/释放,而是依赖属性进行内存管理。我可以看到保持代码清晰并减少出错空间的吸引力,但像这样开放接口让我感到不舒服。总的来说,建筑非常好,但我一直很迂腐地把我的课程与外界封闭起来。
使用这样的属性是 Objective-C 中公认的设计方法吗?任何人都可以向我提供链接或线索,让我的新团队可能在其中采用此策略吗?
I have started work at a new company and one of the guidelines I have been told to adhere to by my team lead is to rarely use retain/release and instead rely on properties for memory management. I can see the appeal of keeping the code clear and leaving less room for mistakes but opening up the interfaces like this makes me uncomfortable. Generally speaking the architecture is very good but I have always been pedantic about closing up my classes to the outside world.
Is using properties like this an accepted design methodology in objective-c? Can anyone provide me with links or a clue where my new team may have picked up this strategy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有必要向全世界公开属性。在您的实现 .m 文件中,您可以添加一个小类别来声明“私有”属性。例如,
从严格意义上来说,Objective-C 中没有什么是真正私有的,所以我想说这是一个很好的实践——它隐藏了几乎所有的保留/释放内容,而不需要 ARC 兼容的运行时,并且具有不需要你的副作用在标题中提及您的实例变量(尽管还有其他方法可以实现这一点)。
作为历史记录,我认为这是将实例变量移出标头的第一种方法 - 这是只有 iOS 和 64 位 Intel 10.6+ 上的“新”运行时才允许的方法 - 因此这可能是您的团队的第二个原因已经决定了。除非他们明确告诉您让您的类透明,否则他们实际上可能完全同意您的感觉(以及广为接受的面向对象原则),即实现应该是不透明的。
There is no need to expose properties to the entire world. In your implementation .m file you can add a little category to declare 'private' properties. E.g.
Nothing in Objective-C is ever really private in strict terms, so I'd say this was good practice — it hides almost all of the retain/release stuff without requiring an ARC-compatible runtime and has the side effect of not requiring you to mention your instance variables in the header at all (though there are other ways to achieve that).
As a historical note, I think this was the first way to move instance variables out of the header — which is something permitted only by the 'new' runtime on iOS and 64bit Intel 10.6+ — so that may be a secondary reason why your team have settled upon it. Unless they've explicitly told you to make your classes transparent, they may actually be completely in agreement with your feeling (and the well accepted object oriented principle) that implementations should be opaque.
您不必公开申报您的财产。使用类类别或类扩展,您可以将属性放置在实现中。
例如:
有关详细信息,请参阅 Apple 的文档。
You don't have to declare your properties publicly. Using a class category or class extension, you can place your properties within the implementation.
For example:
For more information, see Apple's documentation.