objects classes and metaclasses in Objective-C


  Objective-C is a class-based object system. Each object is an instance of some class; the object‘s isa pointer points to its class. That class describes the object‘s data: allocation size and ivar types and layout. The class also describes the object‘s behavior: the selectors it responds to and instance methods it implements.

The class‘s method list is the set of instance methods, the selectors that the object responds to. When you send a message to an instance,objc_msgSend() looks through the method list of that object‘s class (and superclasses, if any) to decide what method to call.

Each Objective-C class is also an object. It has an isa pointer and other data, and can respond to selectors. When you call a "class method" like [NSObject alloc], you are actually sending a message to that class object.

Since a class is an object, it must be an instance of some other class: a metaclass. The metaclass is the description of the class object, just like the class is the description of ordinary instances. In particular, the metaclass‘s method list is the class methods: the selectors that the class object responds to. When you send a message to a class - an instance of a metaclass - objc_msgSend() looks through the method list of the metaclass (and its superclasses, if any) to decide what method to call. Class methods are described by the metaclass on behalf of the class object, just like instance methods are described by the class on behalf of the instance objects.

What about the metaclass? Is it metaclasses all the way down? No. A metaclass is an instance of the root class‘s metaclass; the root metaclass is itself an instance of the root metaclass. The isa chain ends in a cycle here: instance to class to metaclass to root metaclass to itself. The behavior of metaclass isa pointers rarely matters, since in the real world nobody sends messages to metaclass objects.

More important is the superclass of a metaclass. The metaclass‘s superclass chain parallels the class‘s superclass chain, so class methods are inherited in parallel with instance methods. And the root metaclass‘s superclass is the root class, so each class object responds to the root class‘s instance methods. In the end, a class object is an instance of (a subclass of) the root class, just like any other object.

Confused? The diagram may help. Remember, when a message is sent to any object, the method lookup starts with that object‘s isa pointer, then continues up the superclass chain. "Instance methods" are defined by the class, and "class methods" are defined by the metaclass plus the root (non-meta) class.

In proper computer science language theory, a class and metaclass hierarchy can be more free-form, with deeper metaclass chains and multiple classes instantiated from any single metaclass. Objective-C uses metaclasses for practical goals like class methods, but otherwise tends to hide metaclasses. For example, [NSObject class] is identical to [NSObject self], even though in formal terms it ought to return the metaclass that NSObject->isa points to. The Objective-C language is a set of practical compromises; here it limits the class schema before it gets too, well, meta.

  from -> http://www.sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html

objects classes and metaclasses in Objective-C,布布扣,bubuko.com

时间: 2024-08-03 15:37:13

objects classes and metaclasses in Objective-C的相关文章

Python高级特性(3): Classes和Metaclasses(转)

原文:Python高级特性(3): Classes和Metaclasses 类和对象 类和函数一样都是Python中的对象.当一个类定义完成之后,Python将创建一个“类对象”并将其赋值给一个同名变量.类是type类型的对象(是不是有点拗口?). 类对象是可调用的(callable,实现了 __call__方法),并且调用它能够创建类的对象.你可以将类当做其他对象那么处理.例如,你能够给它们的属性赋值,你能够将它们赋值给一个变量,你 可以在任何可调用对象能够用的地方使用它们,比如在一个map中

Classes and metaclasses

http://www.sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html Objective-C is a class-based object system. Each object is an instance of some class; the object's isa pointer points to its class. That class describes t

运行时(iOS)

运行时(iOS) 一.什么是运行时(Runtime)? 运行时是苹果提供的纯C语言的开发库(运行时是一种非常牛逼.开发中经常用到的底层技术) 二.运行时的作用? 能获得某个类的所有成员变量 能获得某个类的所有属性 能获得某个类的所有方法 交换方法实现 能动态添加一个成员变量 能动态添加一个属性 能动态添加一个方法 三.案例:运行时获取成员变量名称 1.分析 #import <Foundation/Foundation.h> #import "XMGPerson.h" #im

iOS运行时 -- Runtime(摘抄自网络)

运行时(iOS) 一.什么是运行时(Runtime)? 运行时是苹果提供的纯C语言的开发库(运行时是一种非常牛逼.开发中经常用到的底层技术) 二.运行时的作用? 能获得某个类的所有成员变量 能获得某个类的所有属性 能获得某个类的所有方法 交换方法实现 能动态添加一个成员变量 能动态添加一个属性 能动态添加一个方法 三.案例:运行时获取成员变量名称 1.分析 #import <Foundation/Foundation.h> #import "XMGPerson.h" #im

杂谈转载

一.什么是运行时(Runtime)? 运行时是苹果提供的纯C语言的开发库(运行时是一种非常牛逼.开发中经常用到的底层技术) 二.运行时的作用? 能获得某个类的所有成员变量 能获得某个类的所有属性 能获得某个类的所有方法 交换方法实现 能动态添加一个成员变量 能动态添加一个属性 能动态添加一个方法 三.案例:运行时获取成员变量名称 1.分析 #import <Foundation/Foundation.h> #import "XMGPerson.h" #import <

iOS 运行时应用

一.什么是运行时(Runtime)? 运行时是苹果提供的纯C语言的开发库(运行时是开发中经常用到的底层技术) 二.运行时的作用? 能获得某个类的所有成员变量 能获得某个类的所有属性 能获得某个类的所有方法 交换方法实现 能动态添加一个成员变量 能动态添加一个属性 能动态添加一个方法 三.案例:运行时获取成员变量名称 1.分析 #import <Foundation/Foundation.h> #import "CKPerson.h" #import <objc/run

Objective-C 的动态提示和技巧

过去的几年中涌现了大量的Objective-C开发者.有些是从动态语言转过来的,比如Ruby或Python,有些是从强类型语言转过来的,如Java或C#,当然也有直接以Objective-C作为入门语言的.也就是说有很大一部分开发者都没有使用Objective-C太长时间.当你接触一门新语言时,更多地会关注基础知识,如语法和特性等.但通常有一些更高级的,更鲜为人知又有强大功能的特性等待你去开拓. The Runtime Objective-C是一门简单的语言,95%是C.只是在语言层面上加了些关

iOS 动态特性和RunTime

过去的几年中涌现了大量的Objective-C开发者.有些是从动态语言转过来的,比如Ruby或Python,有些是从强类型语言转过来的,如Java或C#,当然也有直接以Objective-C作为入门语言的.也就是说有很大一部分开发者都没有使用Objective-C太长时间.当你接触一门新语言时,更多地会关注基础知识,如语法和特性等.但通常有一些更高级的,更鲜为人知又有强大功能的特性等待你去开拓. 这篇文章主要是来领略下Objective-C的运行时(runtime),同时解释是什么让Object

iOS 数据结构

过去的几年中涌现了大量的Objective-C开发者.有些是从动态语言转过来的,比如Ruby或Python,有些是从强类型语言转过来的,如Java或C#,当然也有直接以Objective-C作为入门语言的.也就是说有很大一部分开发者都没有使用Objective-C太长时间.当你接触一门新语言时,更多地会关注基础知识,如语法和特性等.但通常有一些更高级的,更鲜为人知又有强大功能的特性等待你去开拓. 这篇文章主要是来领略下Objective-C的运行时(runtime),同时解释是什么让Object