[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 problem

Fragile 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 ivars

Say you're writing the world's next great pet shop application for Mac OS X Leopard. You might have this PetShopView subclass of NSView, with arrays for the puppies and kittens in the pet shop.

NSView (Leopard)
0 Class isa
4 NSRect bounds
20 NSView *superview
24 NSColor *bgColor
PetShopView
0 Class isa
4 NSRect bounds
20 NSView *superview
24 NSColor *bgColor
28 NSArray *kittens
32 NSArray *puppies

Then Mac OS X Def Leopard comes out, with its new multi-paw interface technology. The AppKit developers add some paw-tracking code to NSView.

NSView (Def Leopard)
0 Class isa
4 NSRect bounds
20 NSView *superview
24 NSColor *bgColor
28 NSSet *touchedPaws
PetShopView
0 Class isa
4 NSRect bounds
20 NSView *superview
24 NSColor *bgColor
28 NSArray *kittens
32 NSArray *puppies

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 ivars

What you and the AppKit developers really want is something like this.

NSView (Def Leopard)
0 Class isa
4 NSRect bounds
20 NSView *superview
24 NSColor *bgColor
28 NSSet *touchedPaws
PetShopView
0 Class isa
4 NSRect bounds
20 NSView *superview
24 NSColor *bgColor
28 NSSet *touchedPaws
32 NSArray *kittens
36 NSArray *puppies

Here, the runtime has recognized that NSView is now larger than it was when PetShopView was compiled. The subclass ivars slide in response, without recompiling any code, and the kittens are saved by a dynamic runtime.

How it works

The generated code for classic Objective-C ivar access works like a C struct field. The offset to the ivar is a constant determined at compile time. The new ivar code instead creates a variable for each ivar which contains the offset to that ivar, and all code that accesses the ivar uses that variable. At launch time, the runtime can change any ivar offset variable if it detects an oversize superclass.

In the pet shop example, _OBJC_IVAR_PetShopView_kittens is 28 at compile time, but the runtime changes it to 32 when it sees the Def Leopard version of NSView. No code needs to be recompiled, and the performance overhead of the extra ivar offset variable is small. AppKit is happy, you're happy, and the kittens are happy.

http://www.sealiesoftware.com/blog/archive/2009/01/27/objc_explain_Non-fragile_ivars.html

[objc explain]: Non-fragile ivars的更多相关文章

  1. 李洪强经典面试题152-Runtime

    李洪强经典面试题152-Runtime   Runtime Runtime是什么 Runtime 又叫运行时,是一套底层的 C 语言 API,其为 iOS 内部的核心之一,我们平时编写的 OC 代码, ...

  2. iOS开发——高级特性&Runtime运行时特性详解

    Runtime运行时特性详解 本文详细整理了 Cocoa 的 Runtime 系统的知识,它使得 Objective-C 如虎添翼,具备了灵活的动态特性,使这门古老的语言焕发生机.主要内容如下: 引言 ...

  3. 刨根问底Objective-C Runtime

    http://chun.tips/blog/2014/11/05/bao-gen-wen-di-objective%5Bnil%5Dc-runtime-(2)%5Bnil%5D-object-and- ...

  4. Objective-C Runtime(转)

    博主地址: http://yulingtianxia.com/blog/2014/11/05/objective-c-runtime/ 曾经觉得Objc特别方便上手,面对着 Cocoa 中大量 API ...

  5. 唐巧的iOS技术博客选摘

    1. 那些被遗漏的objective-c保留字:http://blog.devtang.com/blog/2013/04/29/the-missing-objc-keywords/   2. 使用cr ...

  6. Runtime运行时的那点事儿

    注:本文是对 Colin Wheeler 的 Understanding the Objective-C Runtime 的翻译. 初学 Objective-C(以下简称ObjC) 的人很容易忽略一个 ...

  7. 结合 category 工作原理分析 OC2.0 中的 runtime

    绝大多数 iOS 开发者在学习 runtime 时都阅读过 runtime.h 文件中的这段代码: struct objc_class { Class isa  OBJC_ISA_AVAILABILI ...

  8. iOS开发-Runtime详解(简书)

    简介 Runtime 又叫运行时,是一套底层的 C 语言 API,其为 iOS 内部的核心之一,我们平时编写的 OC 代码,底层都是基于它来实现的.比如: [receiver message]; // ...

  9. Runtime运行时机制

    Runtime 又叫运行时,是一套底层的 C 语言 API,其为 iOS 内部的核心之一,我们平时编写的 OC 代码,底层都是基于它来实现的 我们需要了解的是 Objective-C 是一门动态语言, ...

随机推荐

  1. 网易和淘宝的rem方案剖析

    以下内容到分割线前是引用前端大牛的文章,方便大家理解博主内容): 从网易与淘宝的font-size思考前端设计稿与工作流 1. 简单问题简单解决 我觉得有些web app并一定很复杂,比如拉勾网,你看 ...

  2. sublime text 3支持GBK编码

    1.安装Package Control: 按Ctrl+~打开命令行,然后复制粘贴下面这一行代码,回车确定: import urllib.request,os; pf = 'Package Contro ...

  3. Swift强制解析

    IDE:Xcode Version7.3.1 Swift中"数据类型?"表示这是可选类型,即 某个常量或者变量可能是一个类型,也可能什么都没有,不确定它是否有值,也许会是nil. ...

  4. spring整合springmvc和mybatis

    1.spring 1.1 jar包 1.2 spring基本配置,包扫描注解 <!-- 自动扫描 --> <context:component-scan base-package=& ...

  5. json数据发送时浏览器提示“保存”解决

    数据以json形式发送的时候,部分浏览器不能直接解析,而是提示是否保存,nodejs的express应用中可以通过如下代码解决该问题: router.get('/', function (req, r ...

  6. Tidb 离线Ansible方式部署实践

    1.最近浏览到一个比较新的分布式数据库Tidb,开源看起来比较牛的样子,一时手痒就动手试试部署 2.参考官方 Ansible 离线方式部署 :https://pingcap.com/docs-cn/o ...

  7. Springmvc file多附件上传 显示 删除操作

    之前项目需求要做一个多附件上传 并显示上传文件 带删除操作 一筹莫展之际搜到某个兄弟发的博客感觉非常好用被我copy下来了此贴算是改良版 再次感谢(忘记叫什么了时间也有点久没有历史记录了)先上图 基于 ...

  8. JavaScript常用类

    JS常用类 一.Number 1.常用数字 整数:10 小数:3.14 科学计数法:1e5 | 1e-5 正负无穷:Infinity | -Infinity 2.常用进制 二进制:0b1010 八进制 ...

  9. avalon2简单数据绑定(自定义属性绑定)

    <!DOCTYPE html> <html> <head> <title>项目</title> <meta charset=" ...

  10. .NET开源工作流RoadFlow-流程运行-任务收回

    如果一个任务则发送,又觉得还要想修改可以立即收回刚刚发送的任务. 任务收回条件:任务发送后下一步处理人还没有打开该任务,则在已办事项中会看到 收回 按钮,否则不能收回. 点击收回按钮再确认即可收回刚刚 ...