Does the order of instance variable declaration matter in Objective-C?

拈花ヽ惹草 提交于 2019-12-05 12:38:01

This article is out of date. It was at one time true that ivars were stored in an Objective-C instance just as the members of a struct are stored, and thus that memory alignment could (marginally) affect access time.

Now, however, ivars are indirectly accessed (at least in Apple's runtime); the instance now holds the offset of the ivar, and uses that to access the variable. Since all those offsets are of the same type, and you have no control over the other storage, this alignment issue is obviated.

Further, explicit ivar declaration has fallen out of routine usage with the introduction of declared properties.

The layout of ivars is determined by the Objective-C compiler and there is no control provided in the language to effect this. While the article you've read might have been correct at some point in time for a particular compiler, it is reporting implementation-specific details you cannot rely on.

Trust the compiler to do a good job; or if layout is important to you use C or other language which provides control over layout.

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