Objective-C中的一些特殊的数据类型 SEL、id、@、nil、Nil

来源:互联网 发布:淘宝发布宝贝教程图解 编辑:程序博客网 时间:2024/06/06 09:57

下面是从objc.h中摘录的一段,定义了一些数据类型:

// 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

endif

ifndef nil

define nil 0

endif

SEL:
SEL是”selector”的一个类型, 表示一个方法的名字—-就是一个方法的入口地址

id:
id是一个指向任何一个集成了Object(或者NSObject)类的对象

@:
OC中的指令符

注:需要注意的是id是一个指针, 所以在使用id的适合不需要加*

nil和C语言的NULL相同,在objc/objc.h中定义。nil表示一个Objctive-C对象,这个对象的指针指向空(没有东西就是空)。

首字母大写的Nil和nil有一点不一样,Nil定义一个指向空的类(是Class,而不是对象)

Class
从上文的定义看,Class(类)被定义为一个指向struct objc_class的指针,在objc/objc-class.h中它是这么定义的:

struct objc_class {
struct objc_class *isa;
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;
};

0 0