概念
// objc.h
复制代码
typedef struct objc_class *Class; typedef struct objc_object { Class isa; } *id; typedef struct objc_selector *SEL; typedef id (*IMP)(id, SEL, …); typedef signed char BOOL; #define YES (BOOL)1 #define NO (BOOL)0 #ifndef Nil #define Nil 0 /* id of Nil class */ #endif #ifndef nil #define nil 0 /* id of Nil instance */ #endif id
NSLog (@"SEL=%s", @selector(blah));
复制代码
会输出为 SEL=blah。说白了SEL就是返回方法名。
#define NUM_BUILTIN_SELS 16371
复制代码
/* base-2 log of greatest power of 2 < NUM_BUILTIN_SELS */ #define LG_NUM_BUILTIN_SELS 13 static const char * const _objc_builtin_selectors[NUM_BUILTIN_SELS] = { ".cxx_construct", ".cxx_destruct", "CGColorSpace", "CGCompositeOperationInContext:", "CIContext", "CI_affineTransform", "CI_arrayWithAffineTransform:", "CI_copyWithZone:map:", "CI_initWithAffineTransform:", "CI_initWithRect:", "CI_rect", "CTM", "DOMDocument", "DTD", ... }; 可以看到,数组的大小NUM_BUILTIN_SELS定义为16371。
static struct __objc_sel_set *_objc_selectors = NULL;
复制代码
struct __objc_sel_set { uint32_t _count; uint32_t _capacity; uint32_t _bucketsNum; SEL *_buckets; }; IMP
id (*IMP) (id, SEL, …)。
复制代码
这样说来,IMP是一个指向函数的指针,
typedef struct objc_method *Method;
复制代码
struct objc_method { SEL method_name; char *method_types; IMP method_imp; }; 这个定义看上去包括了我们上面说过的其他类型。
struct objc_class {
复制代码
struct objc_class *isa; /* metaclass */ struct objc_class *super_class; /* 父类 */ const char *name; /* 类名称 */ long version; /* 版本 */ long info; /* 类信息 */ long instance_size; /* 实例大小 */ struct objc_ivar_ list *ivars; /* 实例参数链表 */ struct objc_method_list **methodLists; /* 方法链表 */ struct objc_cache *cache; /* 方法的缓存 */ struct objc_protocol_list *protocols; /* protocol链表 */ }; 由以上的结构信息,我们可以像类似于C语言中结构体操作一样来使用成员。
Class cls;
cls = [NSString class]; printf("class name %s\n", ((struct objc_class*)cls)->name); |
C/C++语言到Objective-C语
从C/C++语言到Objective-C语
2010-12-20 15:55