没有ivars->我想念什么? [英] No ivars -> What am I missing?

查看:78
本文介绍了没有ivars->我想念什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从不使用ivars.我只使用属性-有时为属性分配原始类型,有时在.我已经看到了在切换到ARC时不使用ivars的优点-我借了一些带有很多ivars的代码,但我仍然不能"ARC",因为我不知道需要保留什么内容.因此,我知道使用ivars的一些优点,但是使用ivars代替属性的优点是什么?

I never use ivars. I only use properties -- sometimes assign properties with primitive types, and sometimes on a "private" class extension. I've seen the advantages of not using ivars in switching to ARC -- I have some borrowed code with lots of ivars that I still can't "ARC", since I don't know what needs to be retained. So I know some advantages of not using ivars, but what are the advantages of using ivars instead of properties?

注意:我只依赖于(由编译器?)自动添加到属性声明中的ivars.

Note: I depend exclusively on the ivars that are automagically added in (by the compiler?) for the property declaration.

不要标记为关闭:我查看了其他一些问题,例如,但没有一个出现. 标题看起来不错,但就像SO上的许多问题一样,这些问题是一堆奇怪的疑问和其他东西.

Don't mark to close: I've looked at some of the other questions, like this and this and none hit the spot. The titles look good, but like so many questions on SO, the questions are a mess of strange doubts and other stuff.

推荐答案

声明的属性不能与@protected ivar一样对待.您可以在类扩展中声明该属性,以使其不受任何其他类私有,或者可以在标头接口中声明该属性以使其可公开访问,但是没有办法使它只能由子类访问.这将需要ivar声明.

Declared properties cannot be treated in the same manner as an @protected ivar. You can declare the property in a class extension to keep it private from any other class, or declare it in the header interface to make it publicly accessible, however there is no way to make it accessible only to subclasses. This would require the ivar declaration.

编辑

又是一个简短的想法.我最近一直在写很多框架类,并且我认为使用iVars作为文档可能要说些什么.

Just another brief thought. I have recently been writing a lot of framework classes, and I think there might be something to be said for using iVars as documentation.

例如,假设您正在紧密循环中调用某些代码,并且想要确保其性能.在该紧密循环内,您想访问一个类的属性,但需要知道每次调用它时,返回值是即时计算的还是存储在iVar中.在标头中看到iVar是确保您无需花费太多开销就可以获取该变量的快速方法.

For example, let's say you are calling some code in a tight loop and you want to ensure that it is performant. Inside that tight loop you want to access a property of a class, but need to know whether each time you call it the return value is calculated on-the-fly or stored in an iVar. Seeing the iVar in the header is a quick way to ensure that you'll get that variable back without much overhead.

这篇关于没有ivars->我想念什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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