No ivars -> What am I missing?

為{幸葍}努か 提交于 2019-12-04 03:46:33

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.

EDIT

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.

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.

I don't see any reason to use iVars if you don't have to. If Apple and the compiler want to do work for you, I say let them. You'll have code that more efficient and easier to maintain. At this point iVars are legacy code.

jv42

One good reason for me: an annoying GCC bug, see this other question for a description.

If you're using Clang/LVVM, then you don't have to worry about this bug.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!