struct objc_class 定义

来源:互联网 发布:九阴绝学坐骑升级数据 编辑:程序博客网 时间:2024/05/16 19:54

OC之OBJC2_UNAVAILABLE

 1、What is OBJC2_UNAVAILABLE macro mean?

  

  意即在OBJC2.0中,这些东西将被删除。



遇到了一些使用__attribute__修饰函数和变量的属性方面的代码,不是太了解,很是汗颜,再此做个总结:


GCC使用__attribute__关键字来描述函数,变量和数据类型的属性,用于编译器对源代码的优化。
描述函数属性的几个重要的关键字:

  • void noreturnfun() __attribute__((noreturn));//函数不会返回。
  • void centon()__attribute__((alias("__centon")));//设置函数别名,函数是__cencon,别名是centon.
  • void main_enter()__attribute__((constructor));//main_enter函数在进入main函数前调用
  • void main_exit()__attribute__((destructor));//main_exit函数在main函数返回后调用
  • void fun() __attribute__((noinline));//fun函数不能作为inline函数优化
  • void fun() __attribute__((section("specials”)));//将函数放到specials段中,而不是通常的text段中
  • no_instrument_function、constructor和destructor关键字主要用于剖析(profiling)源代码的。
  • __attribute__(format(archetype,string-index,first-to-check)):format attribute提供了依照printf, scanf, strftime,strfmon类型函数的参数格式对目标函数进行类型的检查.
  • __attribute__((weak)): weak symbol,弱符号. 若存在两个相同的全局符号时,会引发重定义错误.如果使用weak attribute,则当weak symbol和non-weaksymbol同时存在的时候,linker会使用non-weak symbol.若只有weak symbol存在的时候则只使用weaksymbol.
  • __attribute__((deprecated)): deprecated,弃用.如果在源文件在任何地方地方使用deprecated attribute函数,编译器将会发出警告.
  • __attribute__((aligned(ALIGNMENT))):指定变量或结构体最小字节对齐数,以byte为单位.ALIGNMENT:指定的字节对齐操作数. 
  • __attribute__((cleanup(cleanup_function)):当一个变量的作用域消失时,便会执行后面的clean_function函数.
  • __attribute__((packed)):使变量或者是结构体按照最小的对齐方式,对于变量是1byte对齐,对于字段,也就是field指bit对齐. 
上面只是对常见的一些属性操作的解释,对于其他的用法应当参照GCC提供的文档。



前言:iOS的开发语言objective-c,它的真实面目是它不是真正的面向对象语言,而抽象理解为此而已。其实它就是C+,有个公式可以很好地诠释那就是

OC = C + Runtime; 接下来我们就好好讲讲在Runtime下的objc-class。准备资料,objc4-646/runtime。

一:Class定义

    1.1 小小说明一下objc-api.h里的OBJC_ISA_AVAILABILITY:

/*介绍一下__attribute__((deprecated))的作用,__attribute是给函数、变量、类做属性说明的关键字,deprecated是弃用原先的进行兼容

  若是__OBJC2__,原先的类,编译器发出警告*/

#if !defined(OBJC_ISA_AVAILABILITY)#   if __OBJC2__#       define OBJC_ISA_AVAILABILITY  __attribute__((deprecated))#   else#       define OBJC_ISA_AVAILABILITY  /* still available */#   endif#endif

 

 

typedef struct objc_method *Method;typedef struct objc_ivar *Ivar;typedef struct objc_category *Category;typedef struct objc_property *objc_property_t;

 

 

    1.2 接下来就是runtime.h里的Class的定义:

struct objc_class {    Class isa  OBJC_ISA_AVAILABILITY;//每个Class都有一个isa指针    #if !__OBJC2__    Class super_class                                        OBJC2_UNAVAILABLE;//父类    const char *name                                         OBJC2_UNAVAILABLE;//类名    long version                                             OBJC2_UNAVAILABLE;//类版本    long info                                                OBJC2_UNAVAILABLE;//!*!供运行期使用的一些位标识。如:CLS_CLASS (0x1L)表示该类为普通class; CLS_META(0x2L)表示该类为metaclass等(runtime.h中有详细列出)    long instance_size                                       OBJC2_UNAVAILABLE;//实例大小    struct objc_ivar_list *ivars                             OBJC2_UNAVAILABLE;//存储每个实例变量的内存地址    struct objc_method_list **methodLists                    OBJC2_UNAVAILABLE;//!*!根据info的信息确定是类还是实例,运行什么函数方法等    struct objc_cache *cache                                 OBJC2_UNAVAILABLE;//缓存    struct objc_protocol_list *protocols                     OBJC2_UNAVAILABLE;//协议#endif    } OBJC2_UNAVAILABLE;

 

 

 

 

二:class初始化

    Runtime的行为之一就是initialize。在程序运行过程中,它会在你程序中每个类调用一次initialize。这个调用的时间发生在你的类接收到消息之前,但是在它的超类接收到initialize之后。

//在苹果的官方Runtime中有个objc-initialize.m文件,进行解读typedef struct _objc_initializing_classes {    int classesAllocated;//类是否分配存在    Class *metaclasses;//类的父类,如果没有父类那就是自身} _objc_initializing_classes;//初始化一个对象_objc_initializing_classes//将所有的类存储在静态链表中,以待接下来的接收和发送消息static _objc_initializing_classes *_fetchInitializingClassList(BOOL create){    _objc_pthread_data *data;    //list为类链表    _objc_initializing_classes *list;    Class *classes;        data = _objc_fetch_pthread_data(create);    if (data == nil) return nil;        //链表增加类节点    list = data->initializingClasses;    if (list == nil) {        if (!create) {            return nil;        } else {            list = (_objc_initializing_classes *)            _calloc_internal(1, sizeof(_objc_initializing_classes));            data->initializingClasses = list;        }    }    //将创建的classes接在metaclasses后    classes = list->metaclasses;    if (classes == nil) {        // If _objc_initializing_classes exists, allocate metaclass array,        // even if create == NO.        // Allow 4 simultaneous class inits on this thread before realloc.        list->classesAllocated = 4;        classes = (Class *)        _calloc_internal(list->classesAllocated, sizeof(Class));        list->metaclasses = classes;    }    return list;}

 

 

三.runtime下Class的各项操作(重要几个)

    

3.1 add*(增加)        3.1.1 static IMP addMethod(Class cls, SEL name, IMP imp, const char *types, BOOL replace);//增加方法        3.1.2 BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types);//增加类方法        3.1.3 BOOL class_addIvar(Class cls, const char *name, size_t size,uint8_t alignment, const char *type);//增加实例变量        3.1.4 static BOOL _class_addProperty(Class cls, const char *name,const objc_property_attribute_t *attrs, unsigned int count,BOOL replace);//增加属性    3.2 replace*(修改)        3.2.1 IMP class_replaceMethod(Class cls, SEL name, IMP imp, const char *types); //修改方法        3.2.2 void class_replaceProperty(Class cls, const char *name,const objc_property_attribute_t *attrs, unsigned int n);//修改属性    3.3 get*(获取)        3.3.1 static Class getClass(const char *name);//获取类        3.3.2 static ivar_t *getIvar(Class cls, const char *name);//获取类变量(static相当于“+“)        3.3.3 Method class_getInstanceMethod(Class cls, SEL sel);//获取实例方法        3.3.4 static Method _class_getMethod(Class cls, SEL sel);;//获取类方法        3.3.5 static Protocol *getProtocol(const char *name);//增加协议    3.4 set*(设置)        3.4.1 objc_class::setInitialized();//set的initialized初始化        3.4.2 static Class setSuperclass(Class cls, Class newSuper);//设置父类    3.5 其他还有类似于 void *objc_destructInstance(id obj);//摧毁实例对象等等

 

 

四.Class的重要函数

   

4.1 get*(获取)        4.1.1 object_getClass(id obj);        4.1.2 IMP object_getMethodImplementation(id obj, SEL name);//获得实例方法实现        4.1.3 Ivar object_getInstanceVariable(id obj, const char *name, void **value)//获取实例属性    4.2 set*(设置)        4.2.1 Class object_setClass(id obj, Class cls);        4.2.2 Ivar object_setInstanceVariable(id obj, const char *name, void *value);//设置实例属性        4.2.3 void object_setIvar(id obj, Ivar ivar, id value);//设置实例变量    4.3 其他        4.3.1 static void _class_resolveClassMethod(Class cls, SEL sel, id inst);//动态添加类方法,不必在乎方法是否存在        4.3.2 static void _class_resolveInstanceMethod(Class cls, SEL sel, id inst);//动态添加实现方法,不必在乎方法是否存在        4.3.3 unsigned _class_createInstancesFromZone(Class cls, size_t extraBytes, void *zone,id *results, unsigned num_requested);//创建实例存储空间    4.4 消息转发        4.4.1 void    instrumentObjcMessageSends(BOOL flag);//flag传入YES,运行时发送的所有消息都会打印到/tmp/msgSend-xxxx文件里了。


0 0