[objc explain]: Non-fragile ivars (2009-01-27 09:30 PM) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Non-fragile instance variables are a headline feature of the modern Objective-C runtime available on iPhone and 64-bit Mac. They provide framework developers more flexibility without losing binary compatibility, and pave the way for automatically-synthesized property ivars and ivars declared outside a class's interface. The fragile base class problemFragile ivars are a subset of the classic fragile base class problem. In some languages, a superclass cannot be changed without also recompiling all subclasses of that class. For example, adding data members or virtual member functions to a C++ superclass will break binary compatibility with any subclass of that class, even if the added members are private and invisible to the subclass. In classic Objective-C, methods are mostly non-fragile, thanks to dynamic message dispatch. You can freely add methods to a superclass, as long as you don't have name conflicts. But Objective-C ivars are fragile on 32-bit Mac. 32-bit Mac: fragile Objective-C ivarsSay you're writing the world's next great pet shop application for Mac OS X Leopard. You might have this
Then Mac OS X Def Leopard comes out, with its new multi-paw interface technology. The AppKit developers add some paw-tracking code to
Unfortunately, your kittens are doomed by fragile ivars. Alternatively, the AppKit developers are trapped with whatever ivars they chose in Mac OS X 10.0. iPhone and 64-bit Mac: non-fragile Objective-C ivarsWhat you and the AppKit developers really want is something like this.
Here, the runtime has recognized that How it worksThe generated code for classic Objective-C ivar access works like a C In the pet shop example, |
http://www.sealiesoftware.com/blog/archive/2009/01/27/objc_explain_Non-fragile_ivars.html
解决了二进制的兼容性问题。没有 non-fragile ivars 特性,SuperClass 增删了成员变量,SubClass 必须重新编译才能正常运行。原因是 fragile ivars 环境下,成员变量在对象内存中的偏移量是编译阶段确定的,作为常量 hardcode 到指令中(这一点不确定,求证之后回来 update )。如果 SuperClass 增删了成员变量,那么 SubClass 的成员变量在内存中的偏移量就会变成错误的,因为 SubClass 的成员变量是排布在 SuperClass 的成员变量之后的,SuperClass 的成员变量的增删会导致 SubClass 成员变量的后移、迁移,使用原来的 offset 将无法取到正确的成员变量。
————————————————
版权声明:本文为CSDN博主「海洋顶端」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/fly1183989782/article/details/81050782