美文网首页
runtime常用方法

runtime常用方法

作者: jiangamh | 来源:发表于2016-03-05 23:32 被阅读186次
    //object
    
    Class object_getClass(id obj)
    //通过obj对象获取obj对应的类对象
    Class object_setClass(id obj, Class cls)
    //设置obj对象的对应的类对象,也就是obj的isa指针指向的类对象,kvo实际上就是应用了这一点,通过运行时修改了obj对应的类对象
    BOOL object_isClass(id obj)
    //测试obj是否一个类对象
    const char *object_getClassName(id obj)
    //获取obj对象ivar对应成员的值
    id object_getIvar(id obj, Ivar ivar)
    //设置obj对象ivar对应成员的值
    void object_setIvar(id obj, Ivar ivar, id value)
    
    
    //objc
    
    Class objc_getClass(const char *name)
    //通过类名称获取类对象
    Class objc_getMetaClass(const char *name)
    //获取name对应类对象的元类对象
    int objc_getClassList(Class *buffer, int bufferCount)
    //获取所有类对象列表
    Class *objc_copyClassList(unsigned int *outCount)
    //获取所有类对象列表
    
    //class
    
    const char *class_getName(Class cls)
    //获取类对象的类名称
    BOOL class_isMetaClass(Class cls)
    //测试cls类对象是否元类
    Class class_getSuperclass(Class cls)
    //获取cls类对象的父类对象
    Class class_setSuperclass(Class cls, Class newSuper)
    //设置cls类对象的父类
    size_t class_getInstanceSize(Class cls)
    //获取cls实例的大小
    Ivar class_getInstanceVariable(Class cls, const char *name)
    //获取cls类对象name对应的实例成员ivar结构体
    Ivar class_getClassVariable(Class cls, const char *name)
    //获取cls类对象name对应的类成员ivar结构体
    Ivar *class_copyIvarList(Class cls, unsigned int *outCount)
    //获取cls类对象所有成员ivar结构体
    Method class_getInstanceMethod(Class cls, SEL name)
    //获取cls类对象name对应的实例方法结构体
    Method class_getClassMethod(Class cls, SEL name)
    //获取cls类对象name对应类方法结构体
    IMP class_getMethodImplementation(Class cls, SEL name)
    //获取cls类对象name对应方法imp实现
    BOOL class_respondsToSelector(Class cls, SEL sel)
    //测试cls对应的实例是否响应sel对应的方法
    Method *class_copyMethodList(Class cls, unsigned int *outCount)
    //获取cls对应方法列表
    BOOL class_conformsToProtocol(Class cls, Protocol *protocol)
    //测试cls是否遵守protocol协议
    objc_property_t class_getProperty(Class cls, const char *name)
    //获取cls类对象name对应的属性结构体
    objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount)
    //为cls类对象添加新方法
    BOOL class_addMethod(Class cls, SEL name, IMP imp,
                         const char *types)
    //替换cls类对象中name对应方法的实现
    IMP class_replaceMethod(Class cls, SEL name, IMP imp,
                            const char *types)
    //为cls添加新成员
    BOOL class_addIvar(Class cls, const char *name, size_t size,
                       uint8_t alignment, const char *types)
    //为cls添加新属性
    BOOL class_addProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount)
    
    //创建一个新类
    Class objc_allocateClassPair(Class superclass, const char *name,
                                 size_t extraBytes)
    //注册一个新类
    void objc_registerClassPair(Class cls)
    //销毁cls类和与他相关联的元类
    void objc_disposeClassPair(Class cls)
    
    //Method
    struct objc_method {
        SEL method_name
        char *method_types
        IMP method_imp
    }
    
    SEL method_getName(Method m)
    //获取m对应的选择器
    IMP method_getImplementation(Method m)
    //获取m对应的方法实现的imp指针
    const char *method_getTypeEncoding(Method m)
    //获取m方法的对应编码
    unsigned int method_getNumberOfArguments(Method m)
    //获取m方法参数的个数
    char *method_copyReturnType(Method m)
    //copy方法返回值类型
    char *method_copyArgumentType(Method m, unsigned int index)
    //获取m方法index索引参数的类型
    void method_getReturnType(Method m, char *dst, size_t dst_len)
    //获取m方法返回值类型
    void method_getArgumentType(Method m, unsigned int index,
                                char *dst, size_t dst_len)
    //获取方法的参数类型
    IMP method_setImplementation(Method m, IMP imp)
    //设置m方法的具体实现指针
    void method_exchangeImplementations(Method m1, Method m2)
    //交换m1,m2方法对应具体实现的函数指针
    
    //ivar
    
    struct objc_ivar {
        char *ivar_name                                          OBJC2_UNAVAILABLE;
        char *ivar_type                                          OBJC2_UNAVAILABLE;
        int ivar_offset                                          OBJC2_UNAVAILABLE;
    #ifdef __LP64__
        int space                                                OBJC2_UNAVAILABLE;
    #endif
    }                                                            OBJC2_UNAVAILABLE;
    
    const char *ivar_getName(Ivar v)
    //获取v的名称
    const char *ivar_getTypeEncoding(Ivar v)
    //获取v的类型编码
    ptrdiff_t ivar_getOffset(Ivar v)
    //获取v的偏移量
    
    //property
    
    const char *property_getName(objc_property_t property)
    //获取属性的名称
    const char *property_getAttributes(objc_property_t property)
    //
    objc_property_attribute_t *property_copyAttributeList(objc_property_t property, unsigned int *outCount)
    char *property_copyAttributeValue(objc_property_t property, const char *attributeName)
    
    //sel
    const char *sel_getName(SEL sel)
    SEL sel_getUid(const char *str)
    SEL sel_registerName(const char *str)
    BOOL sel_isEqual(SEL lhs, SEL rhs)
    
    //imp
    
    IMP imp_implementationWithBlock(id block)
    //
    id imp_getBlock(IMP anImp)
    BOOL imp_removeBlock(IMP anImp)
    
    //associateObject
    void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
    //设置object对象关联的对象
    id objc_getAssociatedObject(id object, const void *key)
    //获取object关联的对象
    void objc_removeAssociatedObjects(id object)
    //移除object关联的对象
    
    
    
    
    
    
    
    
    
    
    
    
    

    相关文章

      网友评论

          本文标题:runtime常用方法

          本文链接:https://www.haomeiwen.com/subject/jcudkttx.html