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

↘锁芯ラ 提交于 2019-12-10 08:40:53

问题


I was searching the internet for tips to optimizing Objective-C code and came across this link. In the article I saw the note below, which I am not able to understand.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/25556463/does-the-order-of-instance-variable-declaration-matter-in-objective-c

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