美文网首页
Runtime 源码剖析

Runtime 源码剖析

作者: lazy_boy_coder | 来源:发表于2018-03-07 20:17 被阅读0次

    前言

    学习开发只有两件事是值得做下去的。第一是国外人的书,第二是官方的文档。

    Runtime

    SEL

    定义
    /// An opaque type that represents a method selector.
    typedef struct objc_selector *SEL;
    
    sel_getName
    /// Returns the name of the method specified by a given selector.
    // 通过 SEL 返回方法名字
    OBJC_EXPORT const char * _Nonnull sel_getName(SEL _Nonnull sel)
        OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    NSLog(@"--- %s", sel_getName(@selector(testFormat)));
    

    打印:

    TestProject[22016:6798169] --- testFormat
    
    sel_registerName
    /** 
     * Registers a method with the Objective-C runtime system, maps the method 
     * name to a selector, and returns the selector value.
     * 
     * @param str A pointer to a C string. Pass the name of the method you wish to register.
     * 
     * @return A pointer of type SEL specifying the selector for the named method.
     * 
     * @note You must register a method name with the Objective-C runtime system to obtain the
     *  method’s selector before you can add the method to a class definition. If the method name
     *  has already been registered, this function simply returns the selector.
     */
    
    // 注册一个方法,对这个runtime系统返回一个SEL,如果这个方法的名字早已经被注册,那么这个函数只会返回 SEL
    OBJC_EXPORT SEL _Nonnull sel_registerName(const char * _Nonnull str)
        OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    ((void (*)(id, SEL))(void *)objc_msgSend)((id)self, sel_registerName("testFormat"));
    

    打印:

    TestProject[22427:6817642] testFormat
    
    object_getClassName
    /** 
     * Returns the class name of a given object.
     * 
     * @param obj An Objective-C object.
     * 
     * @return The name of the class of which \e obj is an instance.
     */
    // 返回这个实例化类的名字
    OBJC_EXPORT const char * _Nonnull object_getClassName(id _Nullable obj)
        OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
    
    object_getIndexedIvars
    /** 
     * Returns a pointer to any extra bytes allocated with an instance given object.
     * 
     * @param obj An Objective-C object.
     * 
     * @return A pointer to any extra bytes allocated with \e obj. If \e obj was
     *   not allocated with any extra bytes, then dereferencing the returned pointer is undefined.
     * 
     * @note This function returns a pointer to any extra bytes allocated with the instance
     *  (as specified by \c class_createInstance with extraBytes>0). This memory follows the
     *  object's ordinary ivars, but may not be adjacent to the last ivar.
     * @note The returned pointer is guaranteed to be pointer-size aligned, even if the area following
     *  the object's last ivar is less aligned than that. Alignment greater than pointer-size is never
     *  guaranteed, even if the area following the object's last ivar is more aligned than that.
     * @note In a garbage-collected environment, the memory is scanned conservatively.
     */
    // 得到一个
    OBJC_EXPORT void * _Nullable object_getIndexedIvars(id _Nullable obj)
        OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
        OBJC_ARC_UNAVAILABLE;
    
    sel_getUid
    /** 
     * Registers a method name with the Objective-C runtime system.
     * 
     * @param str A pointer to a C string. Pass the name of the method you wish to register.
     * 
     * @return A pointer of type SEL specifying the selector for the named method.
     * 
     * @note The implementation of this method is identical to the implementation of \c sel_registerName.
     * @note Prior to OS X version 10.0, this method tried to find the selector mapped to the given name
     *  and returned \c NULL if the selector was not found. This was changed for safety, because it was
     *  observed that many of the callers of this function did not check the return value for \c NULL.
     */
    // 向 runtime系统根据字符串注册一个方法,返回SEL
    OBJC_EXPORT SEL _Nonnull sel_getUid(const char * _Nonnull str)
        OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    ((void (*)(id, SEL))(void *)objc_msgSend)((id)self, sel_getUid("testFormat"));
    

    打印:

    TestProject[22472:6820361] testFormat
    
    sel_isMapped
    /** 
     * Identifies a selector as being valid or invalid.
     * 
     * @param sel The selector you want to identify.
     * 
     * @return YES if selector is valid and has a function implementation, NO otherwise. 
     * 
     * @warning On some platforms, an invalid reference (to invalid memory addresses) can cause
     *  a crash. 
     */
    // 判断 SEL 是否存在在 SEL Dictionary
    OBJC_EXPORT BOOL sel_isMapped(SEL _Nonnull sel)
        OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    SEL sel;
    if (sel_isMapped(sel)) {
            NSLog(@"有效");
    }
    

    打印:

    没有打印
    

    IMP

    /// A pointer to the function of a method implementation. 
    #if !OBJC_OLD_DISPATCH_PROTOTYPES
    typedef void (*IMP)(void /* id, SEL, ... */ ); 
    #else
    typedef id _Nullable (*IMP)(id _Nonnull, SEL _Nonnull, ...); 
    #endif
    

    IMP 指针指向方法的具体的实现

    Method

    定义
    /// An opaque type that represents a method in a class definition.
    typedef struct objc_method *Method;
    
    方法
    结构
    struct objc_method {
        // 方法名字
        SEL _Nonnull method_name                                 OBJC2_UNAVAILABLE;
        // 方法类型
        char * _Nullable method_types                            OBJC2_UNAVAILABLE;
       // 方法指针
        IMP _Nonnull method_imp                                  OBJC2_UNAVAILABLE;
    } 
    

    class_replaceMethod

    /** 
     * Replaces the implementation of a method for a given class.
     * 
     * @param cls The class you want to modify.
     * @param name A selector that identifies the method whose implementation you want to replace.
     * @param imp The new implementation for the method identified by name for the class identified by cls.
     * @param types An array of characters that describe the types of the arguments to the method. 
     *  Since the function must take at least two arguments—self and _cmd, the second and third characters
     *  must be “@:” (the first character is the return type).
     * 
     * @return The previous implementation of the method identified by \e name for the class identified by \e cls.
     * 
     * @note This function behaves in two different ways:
     *  - If the method identified by \e name does not yet exist, it is added as if \c class_addMethod were called. 
     *    The type encoding specified by \e types is used as given.
     *  - If the method identified by \e name does exist, its \c IMP is replaced as if \c method_setImplementation were called.
     *    The type encoding specified by \e types is ignored.
     */
    // 替换给定类方法的实现,如果方法不存在则会调用class_addMethod添加方法。如果方法标识确实存在那么会调用method_setImplementation设置实现
    OBJC_EXPORT IMP _Nullable
    class_replaceMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp, 
                        const char * _Nullable types) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    class_getInstanceMethod

    /** 
     * Returns a specified instance method for a given class.
     * 
     * @param cls The class you want to inspect.
     * @param name The selector of the method you want to retrieve.
     * 
     * @return The method that corresponds to the implementation of the selector specified by 
     *  \e name for the class specified by \e cls, or \c NULL if the specified class or its 
     *  superclasses do not contain an instance method with the specified selector.
     *
     * @note This function searches superclasses for implementations, whereas \c class_copyMethodList does not.
     */
     // 获取类的实例化方法
    OBJC_EXPORT Method _Nullable
    class_getInstanceMethod(Class _Nullable cls, SEL _Nonnull name)
        OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Method methodI = class_getInstanceMethod([self class], @selector(viewWillAppear:));
    

    打印:

    (lldb) po methodI
    0x000000010ca31528
    

    class_getClassMethod

    /** 
     * Returns a pointer to the data structure describing a given class method for a given class.
     * 
     * @param cls A pointer to a class definition. Pass the class that contains the method you want to retrieve.
     * @param name A pointer of type \c SEL. Pass the selector of the method you want to retrieve.
     * 
     * @return A pointer to the \c Method data structure that corresponds to the implementation of the 
     *  selector specified by aSelector for the class specified by aClass, or NULL if the specified 
     *  class or its superclasses do not contain an instance method with the specified selector.
     *
     * @note Note that this function searches superclasses for implementations, 
     *  whereas \c class_copyMethodList does not.
     */
     // 获取类方法
    OBJC_EXPORT Method _Nullable
    class_getClassMethod(Class _Nullable cls, SEL _Nonnull name)
        OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Method method = class_getClassMethod([Person class], @selector(test));
    

    打印:

    (lldb) po method
    0x0000000106c633d8
    

    class_getMethodImplementation

    /** 
     * Returns the function pointer that would be called if a 
     * particular message were sent to an instance of a class.
     * 
     * @param cls The class you want to inspect.
     * @param name A selector.
     * 
     * @return The function pointer that would be called if \c [object name] were called
     *  with an instance of the class, or \c NULL if \e cls is \c Nil.
     *
     * @note \c class_getMethodImplementation may be faster than \c method_getImplementation(class_getInstanceMethod(cls, name)).
     * @note The function pointer returned may be a function internal to the runtime instead of
     *  an actual method implementation. For example, if instances of the class do not respond to
     *  the selector, the function pointer returned will be part of the runtime's message forwarding machinery.
     */
     // 得到指向方法实现的指针
    OBJC_EXPORT IMP _Nullable
    class_getMethodImplementation(Class _Nullable cls, SEL _Nonnull name) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    IMP imp = class_getMethodImplementation([self class], @selector(viewWillAppear:));
    

    打印:

    (lldb) po imp
    (UIKit`-[UIViewController viewWillAppear:])
    

    class_getMethodImplementation_stret

    /** 
     * Returns the function pointer that would be called if a particular 
     * message were sent to an instance of a class.
     * 
     * @param cls The class you want to inspect.
     * @param name A selector.
     * 
     * @return The function pointer that would be called if \c [object name] were called
     *  with an instance of the class, or \c NULL if \e cls is \c Nil.
     */
     // 返回方法的具体的实现
    OBJC_EXPORT IMP _Nullable
    class_getMethodImplementation_stret(Class _Nullable cls, SEL _Nonnull name) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0)
        OBJC_ARM64_UNAVAILABLE;
    

    代码:

    
    

    打印:

    
    

    class_respondsToSelector

    /** 
     * Returns a Boolean value that indicates whether instances of a class respond to a particular selector.
     * 
     * @param cls The class you want to inspect.
     * @param sel A selector.
     * 
     * @return \c YES if instances of the class respond to the selector, otherwise \c NO.
     * 
     * @note You should usually use \c NSObject's \c respondsToSelector: or \c instancesRespondToSelector: 
     *  methods instead of this function.
     */
     // 返回一个 Boolean 判断该类是否实现了指定的方法
    OBJC_EXPORT BOOL
    class_respondsToSelector(Class _Nullable cls, SEL _Nonnull sel) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    BOOL isYES = class_respondsToSelector([self class], @selector(viewWillAppear:));
    

    打印:

    (lldb) po isYES
    YES
    

    class_copyMethodList

    /** 
     * Describes the instance methods implemented by a class.
     * 
     * @param cls The class you want to inspect.
     * @param outCount On return, contains the length of the returned array. 
     *  If outCount is NULL, the length is not returned.
     * 
     * @return An array of pointers of type Method describing the instance methods 
     *  implemented by the class—any instance methods implemented by superclasses are not included. 
     *  The array contains *outCount pointers followed by a NULL terminator. You must free the array with free().
     * 
     *  If cls implements no instance methods, or cls is Nil, returns NULL and *outCount is 0.
     * 
     * @note To get the class methods of a class, use \c class_copyMethodList(object_getClass(cls), &count).
     * @note To get the implementations of methods that may be implemented by superclasses, 
     *  use \c class_getInstanceMethod or \c class_getClassMethod.
     */
     // 得到类的方法列表
    OBJC_EXPORT Method _Nonnull * _Nullable
    class_copyMethodList(Class _Nullable cls, unsigned int * _Nullable outCount) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    unsigned int count;
    Method *methodList = class_copyMethodList([self class], &count);
    for (unsigned int i = 0; i < count; i++) {
        Method method = methodList[I];
        NSLog(@"%s", sel_getName(method_getName(method)));
    }
    

    打印:

    TestProject[44285:8024690] testFormat
    TestProject[44285:8024690] didReceiveMe
    TestProject[44285:8024690] viewDidLoad
    

    method_setImplementation

    /** 
     * Sets the implementation of a method.
     * 
     * @param m The method for which to set an implementation.
     * @param imp The implemention to set to this method.
     * 
     * @return The previous implementation of the method.
     */
    OBJC_EXPORT IMP _Nonnull
    method_setImplementation(Method _Nonnull m, IMP _Nonnull imp) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Method m1 = class_getInstanceMethod([TestClass class], @selector(testLog1));
    Method m2 = class_getInstanceMethod([TestClass class], @selector(testLog2));
    IMP imp1 = method_getImplementation(m1);
    IMP imp2 = method_getImplementation(m2);
    method_setImplementation(m1, imp2);
    method_setImplementation(m2, imp1);
    

    method_exchangeImplementations

    Method method1 = class_getInstanceMethod([NSString class], @selector(lowercaseString));
    Method method2 = class_getInstanceMethod([NSString class], @selector(uppercaseString));
    method_exchangeImplementations(method1, method2);//交换两个方法后,两个方法的功能相反
    

    Ivar

    定义
    /// An opaque type that represents an instance variable.
    typedef struct objc_ivar *Ivar;
    
    结构
    struct objc_ivar {
        char * _Nullable ivar_name                               OBJC2_UNAVAILABLE;
        char * _Nullable ivar_type                               OBJC2_UNAVAILABLE;
        int ivar_offset                                          OBJC2_UNAVAILABLE;
    #ifdef __LP64__
        int space                                                OBJC2_UNAVAILABLE;
    #endif
    }
    
    方法
    object_getIvar
    /** 
     * Reads the value of an instance variable in an object.
     * 
     * @param obj The object containing the instance variable whose value you want to read.
     * @param ivar The Ivar describing the instance variable whose value you want to read.
     * 
     * @return The value of the instance variable specified by \e ivar, or \c nil if \e object is \c nil.
     * 
     * @note \c object_getIvar is faster than \c object_getInstanceVariable if the Ivar
     *  for the instance variable is already known.
     */
    1. 读取对象中,实例变量的值
    OBJC_EXPORT id _Nullable
    object_getIvar(id _Nullable obj, Ivar _Nonnull ivar) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Person *p1 = [[Person alloc] init];
        Ivar ivar = class_getInstanceVariable([p1 class], "_manTest");
        id mantest = object_getIvar(p1, ivar);
        NSLog(@"%@", mantest);
    

    打印:

    TestProject[22563:6825947] chen test
    
    object_getInstanceVariable
    /** 
     * Obtains the value of an instance variable of a class instance.
     * 
     * @param obj A pointer to an instance of a class. Pass the object containing
     *  the instance variable whose value you wish to obtain.
     * @param name A C string. Pass the name of the instance variable whose value you wish to obtain.
     * @param outValue On return, contains a pointer to the value of the instance variable.
     * 
     * @return A pointer to the \c Ivar data structure that defines the type and name of
     *  the instance variable specified by \e name.
     */
    // 获取实例的实例变量的值
    OBJC_EXPORT Ivar _Nullable
    object_getInstanceVariable(id _Nullable obj, const char * _Nonnull name,
                               void * _Nullable * _Nullable outValue)
        OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
        OBJC_ARC_UNAVAILABLE;
    

    代码:

    Person *p1 = [[Person alloc] init];
    Ivar ivar = class_getInstanceVariable([p1 class], "_manTest");
    
    object_setInstanceVariable
    /** 
     * Changes the value of an instance variable of a class instance.
     * 
     * @param obj A pointer to an instance of a class. Pass the object containing
     *  the instance variable whose value you wish to modify.
     * @param name A C string. Pass the name of the instance variable whose value you wish to modify.
     * @param value The new value for the instance variable.
     * 
     * @return A pointer to the \c Ivar data structure that defines the type and 
     *  name of the instance variable specified by \e name.
     *
     * @note Instance variables with known memory management (such as ARC strong and weak)
     *  use that memory management. Instance variables with unknown memory management 
     *  are assigned as if they were unsafe_unretained.
     */
     //  给类动态的添加一个变量
    OBJC_EXPORT Ivar _Nullable
    object_setInstanceVariable(id _Nullable obj, const char * _Nonnull name,
                               void * _Nullable value)
        OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
        OBJC_ARC_UNAVAILABLE;
    

    代码:

    Person *p1 = [[Person alloc] init];
    object_setInstanceVariableWithStrongDefault(p1, "_manTest", @"shuaibi");
    
    object_setIvar
    /** 
     * Sets the value of an instance variable in an object.
     * 
     * @param obj The object containing the instance variable whose value you want to set.
     * @param ivar The Ivar describing the instance variable whose value you want to set.
     * @param value The new value for the instance variable.
     * 
     * @note Instance variables with known memory management (such as ARC strong and weak)
     *  use that memory management. Instance variables with unknown memory management 
     *  are assigned as if they were unsafe_unretained.
     * @note \c object_setIvar is faster than \c object_setInstanceVariable if the Ivar
     *  for the instance variable is already known.
     */
     // 可以给私有变量设置值 速度超过 object_setInstanceVariable
    OBJC_EXPORT void
    object_setIvar(id _Nullable obj, Ivar _Nonnull ivar, id _Nullable value) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    
    

    代码:

    Person *p1 = [[Person alloc] init];
    Ivar ivar = class_getInstanceVariable([p1 class], "_manTest");
    object_setIvar(p1, ivar, @"shuaibi");
    
    object_setIvarWithStrongDefault
    /** 
     * Sets the value of an instance variable in an object.
     * 
     * @param obj The object containing the instance variable whose value you want to set.
     * @param ivar The Ivar describing the instance variable whose value you want to set.
     * @param value The new value for the instance variable.
     * 
     * @note Instance variables with known memory management (such as ARC strong and weak)
     *  use that memory management. Instance variables with unknown memory management 
     *  are assigned as if they were strong.
     * @note \c object_setIvar is faster than \c object_setInstanceVariable if the Ivar
     *  for the instance variable is already known.
     */
     // 设置变量中实例变量的值 / iOS 10.0+
    OBJC_EXPORT void
    object_setIvarWithStrongDefault(id _Nullable obj, Ivar _Nonnull ivar,
                                    id _Nullable value) 
        OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0);
    

    代码:

    Person *p1 = [[Person alloc] init];
    Ivar ivar = class_getInstanceVariable([p1 class], "_manTest");
    if (@available(iOS 10.0, *)) {
        object_setIvarWithStrongDefault(p1, ivar, @"chen123");
    } else {
        // Fallback on earlier versions
    }
    
    object_setInstanceVariableWithStrongDefault
    /** 
     * Changes the value of an instance variable of a class instance.
     * 
     * @param obj A pointer to an instance of a class. Pass the object containing
     *  the instance variable whose value you wish to modify.
     * @param name A C string. Pass the name of the instance variable whose value you wish to modify.
     * @param value The new value for the instance variable.
     * 
     * @return A pointer to the \c Ivar data structure that defines the type and 
     *  name of the instance variable specified by \e name.
     *
     * @note Instance variables with known memory management (such as ARC strong and weak)
     *  use that memory management. Instance variables with unknown memory management 
     *  are assigned as if they were strong.
     */
     // 在MRC环境下使用,更改实例的实例变量的值
    OBJC_EXPORT Ivar _Nullable
    object_setInstanceVariableWithStrongDefault(id _Nullable obj,
                                                const char * _Nonnull name,
                                                void * _Nullable value)
        OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0)
        OBJC_ARC_UNAVAILABLE;
    

    代码:

    Person *p1 = [[Person alloc] init];
    Ivar ivar = class_getInstanceVariable([p1 class], "_manTest");
    if (@available(iOS 10.0, *)) {
        object_setInstanceVariableWithStrongDefault(p1, "_manTest", @"shuaibi");
    } else {
            // Fallback on earlier versions
    }
    
    object_getInstanceVariable
    /** 
     * Obtains the value of an instance variable of a class instance.
     * 
     * @param obj A pointer to an instance of a class. Pass the object containing
     *  the instance variable whose value you wish to obtain.
     * @param name A C string. Pass the name of the instance variable whose value you wish to obtain.
     * @param outValue On return, contains a pointer to the value of the instance variable.
     * 
     * @return A pointer to the \c Ivar data structure that defines the type and name of
     *  the instance variable specified by \e name.
     */
     // 获取实例变量的值 MRC 环境下有用
    OBJC_EXPORT Ivar _Nullable
    object_getInstanceVariable(id _Nullable obj, const char * _Nonnull name,
                               void * _Nullable * _Nullable outValue)
        OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
        OBJC_ARC_UNAVAILABLE;
    

    代码:

    Person *p1 = [[Person alloc] init];
    Ivar ivar = object_getInstanceVariable(p1, "_manTest", nil);
    NSLog(@" --- %@", object_getIvar(p1, ivar));
    

    打印:

    TestProject[22787:6839305]  --- chen test
    
    ivar_getName
    /* Working with Instance Variables */
    
    /** 
     * Returns the name of an instance variable.
     * 
     * @param v The instance variable you want to enquire about.
     * 
     * @return A C string containing the instance variable's name.
     */
     
     // 返回实时变量的名称
     
    OBJC_EXPORT const char * _Nullable
    ivar_getName(Ivar _Nonnull v) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Person *p1 = [[Person alloc] init];
    Ivar ivar = object_getInstanceVariable(p1, "_manTest", nil);
    NSLog(@"%s", ivar_getName(ivar));
    

    打印:

    TestProject[22909:6849954] _manTest
    
    ivar_getTypeEncoding
    /** 
     * Returns the type string of an instance variable.
     * 
     * @param v The instance variable you want to enquire about.
     * 
     * @return A C string containing the instance variable's type encoding.
     *
     * @note For possible values, see Objective-C Runtime Programming Guide > Type Encodings.
     */
    OBJC_EXPORT const char * _Nullable
    ivar_getTypeEncoding(Ivar _Nonnull v) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Person *p1 = [[Person alloc] init];
    Ivar ivar = object_getInstanceVariable(p1, "_manTest", nil);
    NSLog(@"%s", ivar_getTypeEncoding(ivar));
    

    打印:

    TestProject[23004:6857088] @"NSString"
    
    ivar_getOffset
    /** 
     * Returns the offset of an instance variable.
     * 
     * @param v The instance variable you want to enquire about.
     * 
     * @return The offset of \e v.
     * 
     * @note For instance variables of type \c id or other object types, call \c object_getIvar
     *  and \c object_setIvar instead of using this offset to access the instance variable data directly.
     */
     // 得到实例变量的偏移量
    OBJC_EXPORT ptrdiff_t
    ivar_getOffset(Ivar _Nonnull v) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Person *p1 = [[Person alloc] init];
    Ivar ivar = object_getInstanceVariable(p1, "_manTest", nil);
    NSLog(@"%ld", ivar_getOffset(ivar));
    

    打印:

    TestProject[23488:6907490] 8
    

    Category

    定义

    /// An opaque type that represents a category.
    typedef struct objc_category *Category;
    

    结构

    struct objc_category {
        // 分类名字
        char * _Nonnull category_name                            OBJC2_UNAVAILABLE;
        // 类名
        char * _Nonnull class_name                               OBJC2_UNAVAILABLE;
        // objc 实例方法列表
        struct objc_method_list * _Nullable instance_methods     OBJC2_UNAVAILABLE;
        // objc 类方法列表
        struct objc_method_list * _Nullable class_methods        OBJC2_UNAVAILABLE;
        // 协议列表
        struct objc_protocol_list * _Nullable protocols          OBJC2_UNAVAILABLE;
    }                                                            OBJC2_UNAVAILABLE;
    

    objc_property_t

    定义

    /// An opaque type that represents an Objective-C declared property.
    typedef struct objc_property *objc_property_t;
    

    protocol_getProperty

    /** 
     * Returns the specified property of a given protocol.
     * 
     * @param proto A protocol.
     * @param name The name of a property.
     * @param isRequiredProperty \c YES searches for a required property, \c NO searches for an optional property.
     * @param isInstanceProperty \c YES searches for an instance property, \c NO searches for a class property.
     * 
     * @return The property specified by \e name, \e isRequiredProperty, and \e isInstanceProperty for \e proto,
     *  or \c NULL if none of \e proto's properties meets the specification.
     */
     // 根据协议,属性名字返回一个属性
    OBJC_EXPORT objc_property_t _Nullable
    protocol_getProperty(Protocol * _Nonnull proto,
                         const char * _Nonnull name,
                         BOOL isRequiredProperty, BOOL isInstanceProperty)
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Protocol *protocolA = objc_getProtocol("CatDelegate");
    objc_property_t pro = protocol_getProperty(protocolA, "testMark", YES, YES);
    NSLog(@"%s", property_getName(pro));
    

    打印:

    TestProject[25760:731912] testMark
    

    protocol_copyPropertyList

    /** 
     * Returns an array of the required instance properties declared by a protocol.
     * 
     * @note Identical to 
     * \code
     * protocol_copyPropertyList2(proto, outCount, YES, YES);
     * \endcode
     */
     // 获取一个指定协议的所有的属性,不包括方法
    OBJC_EXPORT objc_property_t _Nonnull * _Nullable
    protocol_copyPropertyList(Protocol * _Nonnull proto,
                              unsigned int * _Nullable outCount)
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Protocol *protocolA = objc_getProtocol("CatDelegate");
    unsigned int outCount;
    objc_property_t *propertyList = protocol_copyPropertyList(protocolA, &outCount);
    for (unsigned int i = 0; i < outCount; i++) {
        objc_property_t property = propertyList[I];
        NSLog(@"name --- %s", property_getName(property));
    }
    

    打印:

    TestProject[26269:782402] name --- testMark
    

    protocol_copyPropertyList2

    /** 
     * Returns an array of properties declared by a protocol.
     * 
     * @param proto A protocol.
     * @param outCount Upon return, contains the number of elements in the returned array.
     * @param isRequiredProperty \c YES returns required properties, \c NO returns optional properties.
     * @param isInstanceProperty \c YES returns instance properties, \c NO returns class properties.
     * 
     * @return A C array of pointers of type \c objc_property_t describing the properties declared by \e proto.
     *  Any properties declared by other protocols adopted by this protocol are not included. The array contains
     *  \c *outCount pointers followed by a \c NULL terminator. You must free the array with \c free().
     *  If the protocol declares no matching properties, \c NULL is returned and \c *outCount is \c 0.
     */
     // 获取协议的所有的属性,分为required 和 optional。
     这个有点鸡肋我试了 optional 不起作用
    OBJC_EXPORT objc_property_t _Nonnull * _Nullable
    protocol_copyPropertyList2(Protocol * _Nonnull proto,
                               unsigned int * _Nullable outCount,
                               BOOL isRequiredProperty, BOOL isInstanceProperty)
        OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0);
    

    代码:

    Protocol *protocolA = objc_getProtocol("CatDelegate");
    unsigned int outCount;
    if (@available(iOS 10.0, *)) {
        objc_property_t *propertyList = protocol_copyPropertyList2(protocolA, &outCount, NO, YES);
        for (unsigned int i = 0; i < outCount; i++) {
            objc_property_t property = propertyList[I];
            NSLog(@"name --- %s", property_getName(property));
        }
    } else {
        // Fallback on earlier versions
    }
    

    打印:

    TestProject[26895:821115] name --- optionalMark
    TestProject[26895:821115] name --- testMark
    

    objc_class

    结构

    struct objc_class {
        Class _Nonnull isa  OBJC_ISA_AVAILABILITY;
    
    #if !__OBJC2__
        Class _Nullable super_class                              OBJC2_UNAVAILABLE;
        const char * _Nonnull name                               OBJC2_UNAVAILABLE;
        long version                                             OBJC2_UNAVAILABLE;
        long info                                                OBJC2_UNAVAILABLE;
        long instance_size                                       OBJC2_UNAVAILABLE;
        struct objc_ivar_list * _Nullable ivars                  OBJC2_UNAVAILABLE;
        struct objc_method_list * _Nullable * _Nullable methodLists                    OBJC2_UNAVAILABLE;
        struct objc_cache * _Nonnull cache                       OBJC2_UNAVAILABLE;
        struct objc_protocol_list * _Nullable protocols          OBJC2_UNAVAILABLE;
    #endif
    
    } OBJC2_UNAVAILABLE;
    

    object_copy

    /** 
     * Returns a copy of a given object.
     * 
     * @param obj An Objective-C object.
     * @param size The size of the object \e obj.
     * 
     * @return A copy of \e obj.
     */
    // 对象的 copy 属于深 copy
    OBJC_EXPORT id _Nullable object_copy(id _Nullable obj, size_t size)
        OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
        OBJC_ARC_UNAVAILABLE;
    

    代码:

    NSLog(@"self --- %p", self);
    NSLog(@"object_copy --- %p", object_copy(self, sizeof(self)));
    

    打印:

    TestProject[23793:6934176] self --- 0x7f9f8c4049e0
    TestProject[23793:6934176] object_copy --- 0x7f9f8c70a1d0
    

    object_dispose

    /** 
     * Frees the memory occupied by a given object.
     * 
     * @param obj An Objective-C object.
     * 
     * @return nil
     */
     // 释放给定对象占用的内存
    OBJC_EXPORT id _Nullable
    object_dispose(id _Nullable obj)
        OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
        OBJC_ARC_UNAVAILABLE;
    

    代码:

    NSLog(@"%@",object_dispose(p1));
    

    打印:

    (lldb) po p1
    <Person: 0x60000000ec50>
    
    2018-03-09 17:15:38.690 YZTestProject[24032:6953790] (null)
    (lldb) po p1
    0x000060000000ec50
    

    object_getClass

    /** 
     * Returns the class of an object.
     * 
     * @param obj The object you want to inspect.
     * 
     * @return The class object of which \e object is an instance, 
     *  or \c Nil if \e object is \c nil.
     */
     //  返回对象的类
    OBJC_EXPORT Class _Nullable
    object_getClass(id _Nullable obj) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Person *p1 = [[Person alloc] init];
    NSLog(@"%@", object_getClass(p1));
    

    打印:

    TestProject[24120:6960612] Person
    
    object_setClass
    /** 
     * Sets the class of an object.
     * 
     * @param obj The object to modify.
     * @param cls A class object.
     * 
     * @return The previous value of \e object's class, or \c Nil if \e object is \c nil.
     */
    OBJC_EXPORT Class _Nullable
    object_setClass(id _Nullable obj, Class _Nonnull cls) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Person *p1 = [[Person alloc] init];
    NSLog(@"\n p1 = %@", p1);
    Class c1 = object_setClass(p1, [Cat class]);
    NSLog(@"\n c1 = %@ \n p1 = %@", c1, p1);
    

    打印:

    TestProject[24490:6987169] 
     p1 = <Person: 0x60000000a580>
    TestProject[24490:6987169] 
     c1 = Person 
     p1 = <Cat: 0x60000000a580>
    
    object_isClass
    /** 
     * Returns whether an object is a class object.
     * 
     * @param obj An Objective-C object.
     * 
     * @return true if the object is a class or metaclass, false otherwise.
     */
     // 判断是否是一个不为空的类对象
    OBJC_EXPORT BOOL
    object_isClass(id _Nullable obj)
        OBJC_AVAILABLE(10.10, 8.0, 9.0, 1.0, 2.0);
    

    代码:

    BOOL isp1 = object_isClass([p1 class]);
    Person * p2;
    BOOL isp2 = object_isClass([p2 class]);
    NSString * p3 = @"test";
    BOOL isp3 = object_isClass([p3 class]);
    NSLog(@"%i", isp1);
    NSLog(@"%i", isp2);
    NSLog(@"%i", isp3);
    

    打印:

    TestProject[24619:6998034] 1
    TestProject[24619:6998034] 0
    TestProject[24619:6998034] 1
    
    objc_getClass
    /* Obtaining Class Definitions */
    
    /** 
     * Returns the class definition of a specified class.
     * 
     * @param name The name of the class to look up.
     * 
     * @return The Class object for the named class, or \c nil
     *  if the class is not registered with the Objective-C runtime.
     * 
     * @note \c objc_getClass is different from \c objc_lookUpClass in that if the class
     *  is not registered, \c objc_getClass calls the class handler callback and then checks
     *  a second time to see whether the class is registered. \c objc_lookUpClass does 
     *  not call the class handler callback.
     * 
     * @warning Earlier implementations of this function (prior to OS X v10.0)
     *  terminate the program if the class does not exist.
     */
    OBJC_EXPORT Class _Nullable
    objc_getClass(const char * _Nonnull name)
        OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Cat *cat1 = [[Cat alloc] init];
    Class c1 = object_getClass(cat1);
    NSLog(@"%@", c1);
    NSLog(@"%@", cat1.name);
    

    打印:

    TestProject[24927:7020395] Cat
    TestProject[24927:7020395] testH
    
    objc_getMetaClass
    /** 
     * Returns the metaclass definition of a specified class.
     * 
     * @param name The name of the class to look up.
     * 
     * @return The \c Class object for the metaclass of the named class, or \c nil if the class
     *  is not registered with the Objective-C runtime.
     * 
     * @note If the definition for the named class is not registered, this function calls the class handler
     *  callback and then checks a second time to see if the class is registered. However, every class
     *  definition must have a valid metaclass definition, and so the metaclass definition is always returned,
     *  whether it’s valid or not.
     */
     // 得到一个类的元类
    OBJC_EXPORT Class _Nullable
    objc_getMetaClass(const char * _Nonnull name)
        OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Cat *cat1 = [[Cat alloc] init];
    Class c1 = objc_getMetaClass("Cat");
    NSLog(@"%@", c1);
    NSLog(@"%@", cat1.name);
    

    打印:

    TestProject[25712:7077004] Cat
    TestProject[25712:7077004] testH
    
    objc_lookUpClass
    /** 
     * Returns the class definition of a specified class.
     * 
     * @param name The name of the class to look up.
     * 
     * @return The Class object for the named class, or \c nil if the class
     *  is not registered with the Objective-C runtime.
     * 
     * @note \c objc_getClass is different from this function in that if the class is not
     *  registered, \c objc_getClass calls the class handler callback and then checks a second
     *  time to see whether the class is registered. This function does not call the class handler callback.
     */
     // 获取指定的类,如果没有注册则返回 nil
    OBJC_EXPORT Class _Nullable
    objc_lookUpClass(const char * _Nonnull name)
        OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Class cl1 = objc_lookUpClass("Ca");
    Class cl2 = objc_lookUpClass("Cat");
    
    objc_getRequiredClass
    /** 
     * Returns the class definition of a specified class.
     * 
     * @param name The name of the class to look up.
     * 
     * @return The Class object for the named class.
     * 
     * @note This function is the same as \c objc_getClass, but kills the process if the class is not found.
     * @note This function is used by ZeroLink, where failing to find a class would be a compile-time link error without ZeroLink.
     */
     // 获取指定类,如果这个类不存在则崩溃
    OBJC_EXPORT Class _Nonnull
    objc_getRequiredClass(const char * _Nonnull name)
        OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Class cl1 = objc_getRequiredClass("Ca");
    
    objc_getClassList
    /** 
     * Obtains the list of registered class definitions.
     * 
     * @param buffer An array of \c Class values. On output, each \c Class value points to
     *  one class definition, up to either \e bufferCount or the total number of registered classes,
     *  whichever is less. You can pass \c NULL to obtain the total number of registered class
     *  definitions without actually retrieving any class definitions.
     * @param bufferCount An integer value. Pass the number of pointers for which you have allocated space
     *  in \e buffer. On return, this function fills in only this number of elements. If this number is less
     *  than the number of registered classes, this function returns an arbitrary subset of the registered classes.
     * 
     * @return An integer value indicating the total number of registered classes.
     * 
     * @note The Objective-C runtime library automatically registers all the classes defined in your source code.
     *  You can create class definitions at runtime and register them with the \c objc_addClass function.
     * 
     * @warning You cannot assume that class objects you get from this function are classes that inherit from \c NSObject,
     *  so you cannot safely call any methods on such classes without detecting that the method is implemented first.
     */
     // 获取所有已经注册的类
    OBJC_EXPORT int
    objc_getClassList(Class _Nonnull * _Nullable buffer, int bufferCount)
        OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Class *bufferClass;
    int numClasses;
    numClasses = objc_getClassList(NULL, 0);
    if(numClasses > 0) {
        bufferClass = (Class *)malloc(sizeof(Class)*numClasses);
        numClasses = objc_getClassList(bufferClass, numClasses);
        NSLog(@"numer of classes: %d", numClasses);
        for (int i = 0; i < numClasses; i++) {
            Class cls = bufferClass[i];
            NSLog(@"class name: %s", class_getName(cls));
        }
        free(bufferClass);
    }
    

    输出结果:

    TestProject[41171:7767871] numer of classes: 4739
    
    objc_copyClassList
    /** 
     * Creates and returns a list of pointers to all registered class definitions.
     * 
     * @param outCount An integer pointer used to store the number of classes returned by
     *  this function in the list. It can be \c nil.
     * 
     * @return A nil terminated array of classes. It must be freed with \c free().
     * 
     * @see objc_getClassList
     */
     // 获取所有已经注册的类
    OBJC_EXPORT Class _Nonnull * _Nullable
    objc_copyClassList(unsigned int * _Nullable outCount)
        OBJC_AVAILABLE(10.7, 3.1, 9.0, 1.0, 2.0);
    

    代码:

    unsigned int outCount;
    Class *classes = objc_copyClassList(&outCount);
    for (int i = 0; i < outCount; i++) {
        NSLog(@"%s", class_getName(classes[i]));
    }
    

    打印:

    TestProject[41322:7782252] JSExport
    TestProject[41322:7782252] NSLeafProxy
    TestProject[41322:7782252] NSProxy
    TestProject[41322:7782252] _UITargetedProxy
    TestProject[41322:7782252] _UIViewServiceReplyControlTrampoline
    
    class_getName
    /* Working with Classes */
    
    /** 
     * Returns the name of a class.
     * 
     * @param cls A class object.
     * 
     * @return The name of the class, or the empty string if \e cls is \c Nil.
     */
     // 返回一个类的名字
    OBJC_EXPORT const char * _Nonnull
    class_getName(Class _Nullable cls) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    NSLog(@"%s", class_getName([self class]));
    

    打印:

    TestProject[41376:7786142] ViewController
    
    class_isMetaClass
    /** 
     * Returns a Boolean value that indicates whether a class object is a metaclass.
     * 
     * @param cls A class object.
     * 
     * @return \c YES if \e cls is a metaclass, \c NO if \e cls is a non-meta class, 
     *  \c NO if \e cls is \c Nil.
     */
     // 判断输出的类是否为元类
    OBJC_EXPORT BOOL
    class_isMetaClass(Class _Nullable cls) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    
    

    代码:

    if (class_isMetaClass(objc_getMetaClass("Cat"))) {
        NSLog(@"YES");
    }
    

    打印:

    TestProject[41512:7792603] YES
    
    class_getSuperclass
    /** 
     * Returns the superclass of a class.
     * 
     * @param cls A class object.
     * 
     * @return The superclass of the class, or \c Nil if
     *  \e cls is a root class, or \c Nil if \e cls is \c Nil.
     *
     * @note You should usually use \c NSObject's \c superclass method instead of this function.
     */
     // 得到这个类的父类
    OBJC_EXPORT Class _Nullable
    class_getSuperclass(Class _Nullable cls) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    NSLog(@"%@", class_getSuperclass([self class]));
    

    打印:

    TestProject[41571:7797664] UIViewController
    
    class_setSuperclass
    /** 
     * Sets the superclass of a given class.
     * 
     * @param cls The class whose superclass you want to set.
     * @param newSuper The new superclass for cls.
     * 
     * @return The old superclass for cls.
     * 
     * @warning You should not use this function.
     */
     // 给类设置新的父类
    OBJC_EXPORT Class _Nonnull
    class_setSuperclass(Class _Nonnull cls, Class _Nonnull newSuper) 
        __OSX_DEPRECATED(10.5, 10.5, "not recommended") 
        __IOS_DEPRECATED(2.0, 2.0, "not recommended") 
        __TVOS_DEPRECATED(9.0, 9.0, "not recommended") 
        __WATCHOS_DEPRECATED(1.0, 1.0, "not recommended")
        __BRIDGEOS_DEPRECATED(2.0, 2.0, "not recommended");
    

    代码:

    NSLog(@"%@", class_getSuperclass(NSClassFromString(@"ViewController")));
        class_setSuperclass(NSClassFromString(@"ViewController"), NSClassFromString(@"UIImageView"));
        NSLog(@"%@", class_getSuperclass(NSClassFromString(@"ViewController")));
    

    打印:

    TestProject[41901:7839232] UIViewController
    TestProject[41901:7839232] UIImageView
    
    class_getVersion
    /** 
     * Returns the version number of a class definition.
     * 
     * @param cls A pointer to a \c Class data structure. Pass
     *  the class definition for which you wish to obtain the version.
     * 
     * @return An integer indicating the version number of the class definition.
     *
     * @see class_setVersion
     */
     // 获取类的版本号  待续...
    OBJC_EXPORT int
    class_getVersion(Class _Nullable cls)
        OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    NSLog(@"%d", class_getVersion([self class]));
    

    打印:

    TestProject[42051:7849333] 0
    
    class_setVersion
    /** 
     * Sets the version number of a class definition.
     * 
     * @param cls A pointer to an Class data structure. 
     *  Pass the class definition for which you wish to set the version.
     * @param version An integer. Pass the new version number of the class definition.
     *
     * @note You can use the version number of the class definition to provide versioning of the
     *  interface that your class represents to other classes. This is especially useful for object
     *  serialization (that is, archiving of the object in a flattened form), where it is important to
     *  recognize changes to the layout of the instance variables in different class-definition versions.
     * @note Classes derived from the Foundation framework \c NSObject class can set the class-definition
     *  version number using the \c setVersion: class method, which is implemented using the \c class_setVersion function.
     */
     // 设置类的版本号
    OBJC_EXPORT void
    class_setVersion(Class _Nullable cls, int version)
        OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    NSLog(@"%d", class_getVersion([self class]));
    class_setVersion([self class], 2);
    NSLog(@"%d", class_getVersion([self class]));
    

    版本:

    TestProject[42247:7871127] 0
    TestProject[42247:7871127] 2
    
    class_getInstanceSize
    /** 
     * Returns the size of instances of a class.
     * 
     * @param cls A class object.
     * 
     * @return The size in bytes of instances of the class \e cls, or \c 0 if \e cls is \c Nil.
     */
     // 返回实例类的大小,以字节为单位
    OBJC_EXPORT size_t
    class_getInstanceSize(Class _Nullable cls) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    NSLog(@"%ld", class_getInstanceSize([self class]));
    

    打印:

    TestProject[42305:7873713] 760
    
    class_getInstanceVariable
    /** 
     * Returns the \c Ivar for a specified instance variable of a given class.
     * 
     * @param cls The class whose instance variable you wish to obtain.
     * @param name The name of the instance variable definition to obtain.
     * 
     * @return A pointer to an \c Ivar data structure containing information about 
     *  the instance variable specified by \e name.
     */
    OBJC_EXPORT Ivar _Nullable
    class_getInstanceVariable(Class _Nullable cls, const char * _Nonnull name)
        OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Person *p1 = [[Person alloc] init];
    Ivar ivar = class_getInstanceVariable([p1 class], "_manTest");
    

    打印:

    (lldb) po ivar
    0x00000001098a1460
    
    class_getClassVariable
    /** 
     * Returns the Ivar for a specified class variable of a given class.
     * 
     * @param cls The class definition whose class variable you wish to obtain.
     * @param name The name of the class variable definition to obtain.
     * 
     * @return A pointer to an \c Ivar data structure containing information about the class variable specified by \e name.
     */
     // 获取参数 cls 类的变量名为name的变量
     
    OBJC_EXPORT Ivar _Nullable
    class_getClassVariable(Class _Nullable cls, const char * _Nonnull name) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Ivar viewIvar = class_getClassVariable([UIView class], "isa");
    id getViewIvar = object_getIvar([UIView class], viewIvar);
    NSLog(@"%d", class_isMetaClass(getViewIvar));
    NSLog(@"%@", getViewIvar);
    

    打印:

    TestProject[42676:7900606] 1
    TestProject[42676:7900606] UIView
    
    class_copyIvarList
    /** 
     * Describes the instance variables declared by a class.
     * 
     * @param cls The class to inspect.
     * @param outCount On return, contains the length of the returned array. 
     *  If outCount is NULL, the length is not returned.
     * 
     * @return An array of pointers of type Ivar describing the instance variables declared by the class. 
     *  Any instance variables declared by superclasses are not included. The array contains *outCount 
     *  pointers followed by a NULL terminator. You must free the array with free().
     * 
     *  If the class declares no instance variables, or cls is Nil, NULL is returned and *outCount is 0.
     */
     // 一个 Ivar 类型的指针数组,用于描述该类声明的实例变量,必须用free释放该数组
    OBJC_EXPORT Ivar _Nonnull * _Nullable
    class_copyIvarList(Class _Nullable cls, unsigned int * _Nullable outCount) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    unsigned int count;
    Ivar *ivars = class_copyIvarList([Person class], &count);
    for (unsigned int i = 0; i < count; i++) {
        Ivar ivar = ivars[I];
        const char *name = ivar_getName(ivar);
        const char *type = ivar_getTypeEncoding(ivar);
        NSLog(@"类型为%s 的 %s", type, name);
        
    }
    free(ivars);
    

    打印:

    TestProject[42937:7913951] 类型为@"NSString" 的 _manTest
    

    Protocol

    定义:

    typedef struct objc_object Protocol;
    

    class_conformsToProtocol

    /** 
     * Returns a Boolean value that indicates whether a class conforms to a given protocol.
     * 
     * @param cls The class you want to inspect.
     * @param protocol A protocol.
     *
     * @return YES if cls conforms to protocol, otherwise NO.
     *
     * @note You should usually use NSObject's conformsToProtocol: method instead of this function.
     */
    // 返回一个 Boolean 值,检查某个类是否遵从某个协议
    OBJC_EXPORT BOOL
    class_conformsToProtocol(Class _Nullable cls, Protocol * _Nullable protocol) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Protocol *protocol = NSProtocolFromString(@"UITableViewDataSource");
    BOOL isConforms = class_conformsToProtocol([UITableViewController class], protocol);
    

    打印:

    (lldb) po isConforms
    YES
    

    class_copyProtocolList

    /** 
     * Describes the protocols adopted by a class.
     * 
     * @param cls The class you want to inspect.
     * @param outCount On return, contains the length of the returned array. 
     *  If outCount is NULL, the length is not returned.
     * 
     * @return An array of pointers of type Protocol* describing the protocols adopted 
     *  by the class. Any protocols adopted by superclasses or other protocols are not included. 
     *  The array contains *outCount pointers followed by a NULL terminator. You must free the array with free().
     * 
     *  If cls adopts no protocols, or cls is Nil, returns NULL and *outCount is 0.
     */
     // 得到一个类遵守协议的个数
    OBJC_EXPORT Protocol * __unsafe_unretained _Nonnull * _Nullable 
    class_copyProtocolList(Class _Nullable cls, unsigned int * _Nullable outCount)
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    unsigned int count;
    Protocol **protocolList = class_copyProtocolList([UITableViewController class], &count);
    for (unsigned int i = 0; i < count; i++) {
        Protocol *pro = protocolList[I];
        NSLog(@"%s", protocol_getName(pro));
    }
    

    打印:

    TestProject[19640:180921] _UIKeyboardAutoRespondingScrollViewController
    TestProject[19640:180921] UITableViewFocusDelegateLegacy
    TestProject[19640:180921] UIViewControllerPreviewingDelegate
    TestProject[19640:180921] UIViewControllerPreviewingDelegate_Deprecated
    TestProject[19640:180921] UITableViewDelegate
    TestProject[19640:180921] UITableViewDataSource
    

    objc_getProtocol

    /* Working with Protocols */
    
    /** 
     * Returns a specified protocol.
     * 
     * @param name The name of a protocol.
     * 
     * @return The protocol named \e name, or \c NULL if no protocol named \e name could be found.
     * 
     * @note This function acquires the runtime lock.
     */
     // 获取名称为 name 的协议
    OBJC_EXPORT Protocol * _Nullable
    objc_getProtocol(const char * _Nonnull name)
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Protocol *protocol = objc_getProtocol("UITableViewDataSource");
    

    打印:

    (lldb) po protocol
    <Protocol: 0x107dbf5e0>
    

    objc_copyProtocolList

    /** 
     * Returns an array of all the protocols known to the runtime.
     * 
     * @param outCount Upon return, contains the number of protocols in the returned array.
     * 
     * @return A C array of all the protocols known to the runtime. The array contains \c *outCount
     *  pointers followed by a \c NULL terminator. You must free the list with \c free().
     * 
     * @note This function acquires the runtime lock.
     */
     // 返回运行时系统所有的协议
    OBJC_EXPORT Protocol * __unsafe_unretained _Nonnull * _Nullable
    objc_copyProtocolList(unsigned int * _Nullable outCount)
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    unsigned int outCount;
    Protocol **protocolList = objc_copyProtocolList(&outCount);
    for (unsigned int i = 0; i < outCount; i++) {
        Protocol *protocol = protocolList[I];
        NSLog(@"%s", protocol_getName(protocol));
    }
    

    打印:

    TestProject[23401:517412] UIVideoEditorControllerDelegate
    TestProject[23401:517412] _UIIVCResponseDelegate
    TestProject[23401:517412] _UITouchable
    TestProject[23401:517412] WebOpenPanelResultListener
    TestProject[23401:517412] SBSRemoteAlertClientHandle
    TestProject[23401:517412] _UIContentContainerInternal
    TestProject[23401:517412] _UIKeyboardAutoRespondingScrollView
    TestProject[23401:517412] CAMLWriterDelegate
    TestProject[23401:517412] OS_nw_endpoint_resolver
    TestProject[23401:517412] _UIActivityHelperDelegate
    

    protocol_conformsToProtocol

    /** 
     * Returns a Boolean value that indicates whether one protocol conforms to another protocol.
     * 
     * @param proto A protocol.
     * @param other A protocol.
     * 
     * @return \c YES if \e proto conforms to \e other, otherwise \c NO.
     * 
     * @note One protocol can incorporate other protocols using the same syntax 
     *  that classes use to adopt a protocol:
     *  \code
     *  @protocol ProtocolName < protocol list >
     *  \endcode
     *  All the protocols listed between angle brackets are considered part of the ProtocolName protocol.
     */
     // 判断一个协议是否遵从了另外一个协议,第一个协议是否遵守了第二个协议
    OBJC_EXPORT BOOL
    protocol_conformsToProtocol(Protocol * _Nullable proto,
                                Protocol * _Nullable other)
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Protocol *tableViewDelegate = objc_getProtocol("UITableViewDelegate");
    Protocol *scrollViewDelegate = objc_getProtocol("UIScrollViewDelegate");
    BOOL isConform = protocol_conformsToProtocol(tableViewDelegate, scrollViewDelegate);
    

    打印:

    (lldb) po isConform
    YES
    点进去,可以看到第一个协议确实遵守了第二个协议
    

    protocol_isEqual

    /** 
     * Returns a Boolean value that indicates whether two protocols are equal.
     * 
     * @param proto A protocol.
     * @param other A protocol.
     * 
     * @return \c YES if \e proto is the same as \e other, otherwise \c NO.
     */
     // 判断两个协议是否相等
    OBJC_EXPORT BOOL
    protocol_isEqual(Protocol * _Nullable proto, Protocol * _Nullable other)
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Protocol *protocolA = objc_getProtocol("UITableViewDataSource");
    Protocol *protocolB = NSProtocolFromString(@"UITableViewDataSource");
    BOOL isEqual = protocol_isEqual(protocolA, protocolB);
    

    打印:

    (lldb) po isEqual
    YES
    

    protocol_getName

    /** 
     * Returns the name of a protocol.
     * 
     * @param p A protocol.
     * 
     * @return The name of the protocol \e p as a C string.
     */
     根据协议得到协议的名称
    OBJC_EXPORT const char * _Nonnull
    protocol_getName(Protocol * _Nonnull proto)
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Protocol *protocolA = objc_getProtocol("UITableViewDataSource");
    NSLog(@"%s", protocol_getName(protocolA));
    

    打印:

    TestProject[23900:551699] UITableViewDataSource
    

    protocol_copyProtocolList

    /** 
     * Returns an array of the protocols adopted by a protocol.
     * 
     * @param proto A protocol.
     * @param outCount Upon return, contains the number of elements in the returned array.
     * 
     * @return A C array of protocols adopted by \e proto. The array contains \e *outCount pointers
     *  followed by a \c NULL terminator. You must free the array with \c free().
     *  If the protocol declares no properties, \c NULL is returned and \c *outCount is \c 0.
     */
     // 返回协议所遵守的协议,以数组的形式展现
    OBJC_EXPORT Protocol * __unsafe_unretained _Nonnull * _Nullable
    protocol_copyProtocolList(Protocol * _Nonnull proto,
                              unsigned int * _Nullable outCount)
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    Protocol *protocolA = objc_getProtocol("CatDelegate");
    unsigned int outCount;
    Protocol **protocolList = protocol_copyProtocolList(protocolA, &outCount);
    for (unsigned int i = 0; i < outCount; i++) {
        Protocol *protocol = protocolList[I];
        NSLog(@"name --- %s", protocol_getName(protocol));
    }
    

    打印:

    TestProject[26997:828030] name --- NSObject
    

    objc_allocateProtocol

    /** 
     * Creates a new protocol instance that cannot be used until registered with
     * \c objc_registerProtocol()
     * 
     * @param name The name of the protocol to create.
     *
     * @return The Protocol instance on success, \c nil if a protocol
     *  with the same name already exists. 
     * @note There is no dispose method for this. 
     */
     // 创建一个新的协议实例,但是需要通过 objc_registerProtocol 注册使用
    OBJC_EXPORT Protocol * _Nullable
    objc_allocateProtocol(const char * _Nonnull name) 
        OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
    

    代码:

    Protocol *protocol = objc_allocateProtocol("PersonDelegate");
    

    打印:

    (lldb) po protocol
    <__IncompleteProtocol: 0x6080000a84c0>
    

    objc_registerProtocol

    /** 
     * Registers a newly constructed protocol with the runtime. The protocol
     * will be ready for use and is immutable after this.
     * 
     * @param proto The protocol you want to register.
     */
     // 注册协议
    OBJC_EXPORT void
    objc_registerProtocol(Protocol * _Nonnull proto) 
        OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
    

    代码:

    Protocol *protocol = objc_allocateProtocol("PersonDelegate");
    // 声明这个协议还是不可以用的,还是要注册这个协议的
    objc_registerProtocol(protocol);
    

    打印:

    (lldb) po protocol
    <Protocol: 0x6080000a84c0>
    

    protocol_addMethodDescription

    /** 
     * Adds a method to a protocol. The protocol must be under construction.
     * 
     * @param proto The protocol to add a method to.
     * @param name The name of the method to add.
     * @param types A C string that represents the method signature.
     * @param isRequiredMethod YES if the method is not an optional method.
     * @param isInstanceMethod YES if the method is an instance method. 
     */
     // 给没有创建的协议添加指定的方法,已经注册的协议无法添加
    OBJC_EXPORT void
    protocol_addMethodDescription(Protocol * _Nonnull proto, SEL _Nonnull name,
                                  const char * _Nullable types,
                                  BOOL isRequiredMethod, BOOL isInstanceMethod) 
        OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
    

    代码:

    Protocol *protocolB = objc_allocateProtocol("PersonDelegate");
    protocol_addMethodDescription(protocolB, @selector(runtimeTestAction3), "v@:", NO, YES);
    unsigned int outCount;
    struct objc_method_description *descList = protocol_copyMethodDescriptionList(protocolB, NO, YES, &outCount);
    for (unsigned int i = 0; i < outCount; i++) {
        struct objc_method_description desc = descList[I];
        char *types = desc.types;
        NSLog(@"name: %@, type: %s", NSStringFromSelector(desc.name), types);
    }
    

    打印:

    TestProject[29062:1027253] name: runtimeTestAction3, type: v@:
    

    protocol_addProtocol

    /** 
     * Adds an incorporated protocol to another protocol. The protocol being
     * added to must still be under construction, while the additional protocol
     * must be already constructed.
     * 
     * @param proto The protocol you want to add to, it must be under construction.
     * @param addition The protocol you want to incorporate into \e proto, it must be registered.
     */
     // 添加一个已注册的协议到未注册的协议当中
     proto:未注册的协议
     addition:被添加的已注册协议
     
    OBJC_EXPORT void
    protocol_addProtocol(Protocol * _Nonnull proto, Protocol * _Nonnull addition) 
        OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
    

    代码:

    // 声明一个协议返回协议的实例
    Protocol *protocol = objc_allocateProtocol("ProjectDelegate");
    objc_registerProtocol(protocol);   // 协议方法已经注册
        
    Protocol *testProtocol = objc_allocateProtocol("TestProtocol");
    protocol_addProtocol(testProtocol, protocol);
        
    // 打印这个协议遵循的协议组
    unsigned int count;
    Protocol **protocolList = protocol_copyProtocolList(testProtocol, &count);
    for (unsigned int i = 0; i < count; i++) {
        Protocol *protocolC = protocolList[I];
        NSLog(@"name --- %s", protocol_getName(protocolC));
    }
    

    打印:

    TestProject[32945:1206976] name --- ProjectDelegate
    

    protocol_addProperty

    /** 
     * Adds a property to a protocol. The protocol must be under construction. 
     * 
     * @param proto The protocol to add a property to.
     * @param name The name of the property.
     * @param attributes An array of property attributes.
     * @param attributeCount The number of attributes in \e attributes.
     * @param isRequiredProperty YES if the property (accessor methods) is not optional. 
     * @param isInstanceProperty YES if the property (accessor methods) are instance methods. 
     *  This is the only case allowed fo a property, as a result, setting this to NO will 
     *  not add the property to the protocol at all. 
     */
     //
    OBJC_EXPORT void
    protocol_addProperty(Protocol * _Nonnull proto, const char * _Nonnull name,
                         const objc_property_attribute_t * _Nullable attributes,
                         unsigned int attributeCount,
                         BOOL isRequiredProperty, BOOL isInstanceProperty)
        OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
    

    代码:

        // 创建新的协议
        Protocol *newProtocol = objc_allocateProtocol("DataDelegate");
        
        // 给一个协议添加一个属性
        objc_property_attribute_t attr_T = {"T", "@\"NSString\""};   // @encod
        objc_property_attribute_t attr_N = {"N", ""};  // 原子性
        objc_property_attribute_t attr_C = {"C", ""};  // copy
        objc_property_attribute_t attr_V = {"V", "_age"}; // 实例变量
        objc_property_attribute_t attrs[] = {attr_T, attr_N, attr_C, attr_V};
        protocol_addProperty(newProtocol, "age", attrs, 4, YES, YES);
        
        // 打印协议的属性
        unsigned int outCount;
        objc_property_t *propertyList = protocol_copyPropertyList(newProtocol, &outCount);
        for (unsigned int i = 0; i < outCount; i++) {
            objc_property_t property = propertyList[I];
            unsigned propertyCount;
            objc_property_attribute_t *attrList = property_copyAttributeList(property, &propertyCount);
            for (unsigned int j  = 0; j < propertyCount; j++) {
                objc_property_attribute_t attr = attrList[j];
                const char *name = attr.name;
                const char *value = attr.value;
                NSLog(@"name: %s, value: %s", name, value);
            }
        }
    

    打印:

    TestProject[33846:1268389] name: T, value: @"NSString"
    TestProject[33846:1268389] name: N, value: 
    TestProject[33846:1268389] name: C, value: 
    TestProject[33846:1268389] name: V, value: _age
    

    objc_method_description

    结构:

    /// Defines a method
    struct objc_method_description {
        SEL _Nullable name;               /**< The name of the method */
        char * _Nullable types;           /**< The types of the method arguments */
    };
    

    protocol_getMethodDescription

    /** 
     * Returns a method description structure for a specified method of a given protocol.
     * 
     * @param p A protocol.
     * @param aSel A selector.
     * @param isRequiredMethod A Boolean value that indicates whether aSel is a required method.
     * @param isInstanceMethod A Boolean value that indicates whether aSel is an instance method.
     * 
     * @return An \c objc_method_description structure that describes the method specified by \e aSel,
     *  \e isRequiredMethod, and \e isInstanceMethod for the protocol \e p.
     *  If the protocol does not contain the specified method, returns an \c objc_method_description structure
     *  with the value \c {NULL, \c NULL}.
     * 
     * @note This function recursively searches any protocols that this protocol conforms to.
     */
     // 返回协议中指定方法的描述
    OBJC_EXPORT struct objc_method_description
    protocol_getMethodDescription(Protocol * _Nonnull proto, SEL _Nonnull aSel,
                                  BOOL isRequiredMethod, BOOL isInstanceMethod)
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    struct objc_method_description desc = protocol_getMethodDescription(protocolA, @selector(numberOfSectionsInTableView:), NO, YES);
    

    打印:

    (lldb) po desc.name
    "numberOfSectionsInTableView:"
    
    (lldb) po desc.types
    "q24@0:8@16"
    

    protocol_copyMethodDescriptionList

    /** 
     * Returns an array of method descriptions of methods meeting a given specification for a given protocol.
     * 
     * @param p A protocol.
     * @param isRequiredMethod A Boolean value that indicates whether returned methods should
     *  be required methods (pass YES to specify required methods).
     * @param isInstanceMethod A Boolean value that indicates whether returned methods should
     *  be instance methods (pass YES to specify instance methods).
     * @param outCount Upon return, contains the number of method description structures in the returned array.
     * 
     * @return A C array of \c objc_method_description structures containing the names and types of \e p's methods 
     *  specified by \e isRequiredMethod and \e isInstanceMethod. The array contains \c *outCount pointers followed
     *  by a \c NULL terminator. You must free the list with \c free().
     *  If the protocol declares no methods that meet the specification, \c NULL is returned and \c *outCount is 0.
     * 
     * @note Methods in other protocols adopted by this protocol are not included.
     */
     // 得到方法描述列表
    OBJC_EXPORT struct objc_method_description * _Nullable
    protocol_copyMethodDescriptionList(Protocol * _Nonnull proto,
                                       BOOL isRequiredMethod,
                                       BOOL isInstanceMethod,
                                       unsigned int * _Nullable outCount)
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    unsigned int outCount;
    struct objc_method_description *descList = protocol_copyMethodDescriptionList(protocolA, YES, YES, &outCount);
    for (unsigned int i = 0; i < outCount; i++) {
        struct objc_method_description desc = descList[I];
        char *types = desc.types;
        NSLog(@"name: %@, type: %s", NSStringFromSelector(desc.name), types);
    }
    

    打印:

    TestProject[25150:681145] name: tableView:numberOfRowsInSection:, type: q32@0:8@16q24
    TestProject[25150:681145] name: tableView:cellForRowAtIndexPath:, type: @32@0:8@16@24
    

    objc_property_t

    定义:

    /// An opaque type that represents an Objective-C declared property.
    typedef struct objc_property *objc_property_t;
    

    class_getProperty

    /** 
     * Returns a property with a given name of a given class.
     * 
     * @param cls The class you want to inspect.
     * @param name The name of the property you want to inspect.
     * 
     * @return A pointer of type \c objc_property_t describing the property, or
     *  \c NULL if the class does not declare a property with that name, 
     *  or \c NULL if \e cls is \c Nil.
     */
     // 返回一个属性,通过参数类,和名字
    OBJC_EXPORT objc_property_t _Nullable
    class_getProperty(Class _Nullable cls, const char * _Nonnull name)
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    objc_property_t property_t = class_getProperty([Person class], "age");
    const char *name = property_getName(property_t);
    NSLog(@"%s", name);
    

    打印:

    TestProject[20119:217782] age
    

    class_copyPropertyList

    /** 
     * Describes the properties declared by a class.
     * 
     * @param cls The class you want to inspect.
     * @param outCount On return, contains the length of the returned array. 
     *  If \e outCount is \c NULL, the length is not returned.        
     * 
     * @return An array of pointers of type \c objc_property_t describing the properties 
     *  declared by the class. Any properties declared by superclasses are not included. 
     *  The array contains \c *outCount pointers followed by a \c NULL terminator. You must free the array with \c free().
     * 
     *  If \e cls declares no properties, or \e cls is \c Nil, returns \c NULL and \c *outCount is \c 0.
     */
     // 得到类属性列表
    OBJC_EXPORT objc_property_t _Nonnull * _Nullable
    class_copyPropertyList(Class _Nullable cls, unsigned int * _Nullable outCount)
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    unsigned int propertyCount;
    objc_property_t *propertyList = class_copyPropertyList([Person class], &propertyCount);
    for (unsigned int i = 0; i < propertyCount; i++) {
        objc_property_t property_t = propertyList[I];
        NSLog(@"%s", property_getName(property_t));
    }
    free(propertyList);
    

    打印:

    TestProject[20198:223479] manTest
    TestProject[20198:223479] age
    

    class_getIvarLayout

    /** 
     * Returns a description of the \c Ivar layout for a given class.
     * 
     * @param cls The class to inspect.
     * 
     * @return A description of the \c Ivar layout for \e cls.
     */
     // 返回一个类的修饰属性
    OBJC_EXPORT const uint8_t * _Nullable
    class_getIvarLayout(Class _Nullable cls)
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    const uint8_t *array_s = class_getIvarLayout([Person class]);
    

    打印:

    (lldb) po array_s
    "\x01"
    

    class_getWeakIvarLayout

    /** 
     * Returns a description of the layout of weak Ivars for a given class.
     * 
     * @param cls The class to inspect.
     * 
     * @return A description of the layout of the weak \c Ivars for \e cls.
     */
     //  待续
    OBJC_EXPORT const uint8_t * _Nullable
    class_getWeakIvarLayout(Class _Nullable cls)
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    
    

    打印:

    
    

    property_getName

    /* Working with Properties */
    
    /** 
     * Returns the name of a property.
     * 
     * @param property The property you want to inquire about.
     * 
     * @return A C string containing the property's name.
     */
     // 返回属性的名字
    OBJC_EXPORT const char * _Nonnull
    property_getName(objc_property_t _Nonnull property) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    objc_property_t property_t = class_getProperty([Person class], "age");
    const char *name = property_getName(property_t);
    NSLog(@"%s", name);
    

    打印:

    TestProject[21844:408300] age
    

    property_getAttributes

    /** 
     * Returns the attribute string of a property.
     * 
     * @param property A property.
     *
     * @return A C string containing the property's attributes.
     * 
     * @note The format of the attribute string is described in Declared Properties in Objective-C Runtime Programming Guide.
     */
     // 返回属性的属性字符串
    OBJC_EXPORT const char * _Nullable
    property_getAttributes(objc_property_t _Nonnull property) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    id lenderClass = objc_getClass("Person");
    unsigned int outCount, I;
    objc_property_t *properties = class_copyPropertyList(lenderClass, &outCount);
    for (unsigned int i = 0; i < outCount; i++) {
        objc_property_t property = properties[I];
        fprintf(stdout, "%s %s\n", property_getName(property), property_getAttributes(property));
    }
    

    打印:

    manTest T@"NSString",&,N,V_manTest
    age Tq,N,V_age
    

    打印解释:

    image

    objc_property_attribute_t

    结构:

    /// Defines a property attribute
    typedef struct {
        const char * _Nonnull name;           /**< The name of the attribute */
        const char * _Nonnull value;          /**< The value of the attribute (usually empty) */
    } objc_property_attribute_t;
    

    property_copyAttributeList

    /** 
     * Returns an array of property attributes for a property. 
     * 
     * @param property The property whose attributes you want copied.
     * @param outCount The number of attributes returned in the array.
     * 
     * @return An array of property attributes; must be free'd() by the caller. 
     */
     // 返回属性的属性列表值
    OBJC_EXPORT objc_property_attribute_t * _Nullable
    property_copyAttributeList(objc_property_t _Nonnull property,
                               unsigned int * _Nullable outCount)
        OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
    

    代码:

    id lenderClass = objc_getClass("Person");
    unsigned int outCount;
    objc_property_t *properties = class_copyPropertyList(lenderClass, &outCount);
    for (unsigned int i = 0; i < outCount; i++) {
        objc_property_t property = properties[I];
        fprintf(stdout, "%s %s\n", property_getName(property), property_getAttributes(property));
            
        unsigned int attrCount = 0;
        objc_property_attribute_t *attrs = property_copyAttributeList(property, &attrCount);
        for (unsigned int j = 0; j < attrCount; j++) {
            objc_property_attribute_t attr = attrs[j];
            const char *name = attr.name;
            const char *value = attr.value;
            NSLog(@"属性的描述: %s 值: %s", name, value);
        }
    }
    

    打印:

    manTest T@"NSString",&,N,V_manTest
    TestProject[23001:481165] 属性的描述: T 值: @"NSString"
    TestProject[23001:481165] 属性的描述: & 值: 
    TestProject[23001:481165] 属性的描述: N 值: 
    TestProject[23001:481165] 属性的描述: V 值: _manTest
    
    age Tq,N,V_age
    TestProject[23001:481165] 属性的描述: T 值: q
    TestProject[23001:481165] 属性的描述: N 值: 
    TestProject[23001:481165] 属性的描述: V 值: _age
    

    /** 
     * Returns the value of a property attribute given the attribute name.
     * 
     * @param property The property whose attribute value you are interested in.
     * @param attributeName C string representing the attribute name.
     *
     * @return The value string of the attribute \e attributeName if it exists in
     *  \e property, \c nil otherwise. 
     */
     // 通过属性的名字得到属性的值
    OBJC_EXPORT char * _Nullable
    property_copyAttributeValue(objc_property_t _Nonnull property,
                                const char * _Nonnull attributeName)
        OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
    

    代码:

    id lenderClass = objc_getClass("Person");
    unsigned int outCount;
    objc_property_t *properties = class_copyPropertyList(lenderClass, &outCount);
    for (unsigned int i = 0; i < outCount; i++) {
        objc_property_t property = properties[I];
        fprintf(stdout, "%s %s\n", property_getName(property), property_getAttributes(property));
            
        unsigned int attrCount = 0;
        objc_property_attribute_t *attrs = property_copyAttributeList(property, &attrCount);
        for (unsigned int j = 0; j < attrCount; j++) {
            objc_property_attribute_t attr = attrs[j];
            const char *name = attr.name;
            const char *value = attr.value;
            const char *test = property_copyAttributeValue(property, name);
            NSLog(@"属性的描述: %s 值: %s", name, value);
            NSLog(@"test -- %s", test);
        }
    }
    

    打印:

    manTest T@"NSString",&,N,V_manTest
    TestProject[23099:496137] 属性的描述: T 值: @"NSString"
    TestProject[23099:496137] test -- @"NSString"
    TestProject[23099:496137] 属性的描述: & 值: 
    TestProject[23099:496137] test -- 
    TestProject[23099:496137] 属性的描述: N 值: 
    TestProject[23099:496137] test -- 
    TestProject[23099:496137] 属性的描述: V 值: _manTest
    TestProject[23099:496137] test -- _manTest
    
    age Tq,N,V_age
    TestProject[23099:496137] 属性的描述: T 值: q
    TestProject[23099:496137] test -- q
    TestProject[23099:496137] 属性的描述: N 值: 
    TestProject[23099:496137] test -- 
    TestProject[23099:496137] 属性的描述: V 值: _age
    TestProject[23099:496137] test -- _age
    
    

    动态库

    objc_copyImageNames

    /* Working with Libraries */
    
    /** 
     * Returns the names of all the loaded Objective-C frameworks and dynamic
     * libraries.
     * 
     * @param outCount The number of names returned.
     * 
     * @return An array of C strings of names. Must be free()'d by caller.
     */
     // 返回所有已加载的 Objective-C 框架和动态库的名称
    OBJC_EXPORT const char * _Nonnull * _Nonnull
    objc_copyImageNames(unsigned int * _Nullable outCount) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    unsigned int outCount;
    const char **nameList = objc_copyImageNames(&outCount);
    for (unsigned int i = 0; i < outCount; i++) {
        const char *name = nameList[I];
        NSLog(@"name : %s", name);
    }
    free(nameList);
    

    打印:

    TestProject[34100:1278609] name : /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/introspection/libdispatch.dylib
    TestProject[34100:1278609] name : /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/libxpc.dylib
    TestProject[34100:1278609] name : /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/libsystem_trace.dylib
    TestProject[34100:1278609] name : /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/libsystem_network.dylib
    TestProject[34100:1278609] name : /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libobjc.A.dylib
    TestProject[34100:1278609] name : /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libate.dylib
    

    class_getImageName

    /** 
     * Returns the dynamic library name a class originated from.
     * 
     * @param cls The class you are inquiring about.
     * 
     * @return The name of the library containing this class.
     */
     // 返回这个类 源动态库名称
    OBJC_EXPORT const char * _Nullable
    class_getImageName(Class _Nullable cls) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    代码:

    const char *libraryName = class_getImageName([UIViewController class]);
    NSLog(@"name: %s", libraryName);
    

    打印:

    TestProject[34267:1284976] name: /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/UIKit.framework/UIKit
    

    objc_copyClassNamesForImage

    
    

    代码:

    unsigned int outCount;
    const char **nameList = objc_copyClassNamesForImage("/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/UIKit.framework/UIKit", &outCount);
    for (unsigned i = 0; i < outCount; i++) {
        const char *name = nameList[I];
        NSLog(@"name: %s", name);
    }
    

    打印:

    部分数据:
    TestProject[34509:1304491] name: _UIPreviewPresentationPlatterView
    TestProject[34509:1304491] name: UIKeyboardUISettings
    TestProject[34509:1304491] name: _UIPickerViewTopFrame
    TestProject[34509:1304491] name: _UIOnePartImageView
    TestProject[34509:1304491] name: _UIPickerViewSelectionBar
    TestProject[34509:1304491] name: _UIPickerWheelView
    TestProject[34509:1304491] name: _UIPickerViewTestParameters
    TestProject[34509:1304491] name: UIPickerView
    

    objc_setAssociatedObject

    /** 
     * Sets an associated value for a given object using a given key and association policy.
     * 
     * @param object The source object for the association.
     * @param key The key for the association.
     * @param value The value to associate with the key key for object. Pass nil to clear an existing association.
     * @param policy The policy for the association. For possible values, see “Associative Object Behaviors.”
     * 
     * @see objc_setAssociatedObject
     * @see objc_removeAssociatedObjects
     */
     // 给已经存在的类添加私有属性,并且设置关联级别
    OBJC_EXPORT void
    objc_setAssociatedObject(id _Nonnull object, const void * _Nonnull key,
                             id _Nullable value, objc_AssociationPolicy policy)
        OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0);
    

    代码:

    UIImage+AddProperty.h
    #import <UIKit/UIKit.h>
    @interface UIImage (AddProperty)
    @property(nonatomic,strong)NSString *imageWidth;
    @end
    
    UIImage+AddProperty.m
    #import "UIImage+AddProperty.h"
    #import <objc/runtime.h>
    
    @implementation UIImage (AddProperty)
    
    -(NSString *)imageWidth{
        return objc_getAssociatedObject(self, _cmd);
    }
    
    -(void)setImageWidth:(NSString *)imageWidth{
        objc_setAssociatedObject(self, @selector(imageWidth), imageWidth, OBJC_ASSOCIATION_ASSIGN);
    }
    
    @end
    

    objc_getAssociatedObject

    /** 
     * Returns the value associated with a given object for a given key.
     * 
     * @param object The source object for the association.
     * @param key The key for the association.
     * 
     * @return The value associated with the key \e key for \e object.
     * 
     * @see objc_setAssociatedObject
     */
     // 通过对象和 key 得到一个关联值
    OBJC_EXPORT id _Nullable
    objc_getAssociatedObject(id _Nonnull object, const void * _Nonnull key)
        OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0);
    

    objc_removeAssociatedObjects

    /** 
     * Removes all associations for a given object.
     * 
     * @param object An object that maintains associated objects.
     * 
     * @note The main purpose of this function is to make it easy to return an object 
     *  to a "pristine state”. You should not use this function for general removal of
     *  associations from objects, since it also removes associations that other clients
     *  may have added to the object. Typically you should use \c objc_setAssociatedObject 
     *  with a nil value to clear an association.
     * 
     * @see objc_setAssociatedObject
     * @see objc_getAssociatedObject
     */
     // 删除给定对象的所有的关联
    OBJC_EXPORT void
    objc_removeAssociatedObjects(id _Nonnull object)
        OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0);
    

    Class

    objc_allocateClassPair

    /* Adding Classes */
    
    /** 
     * Creates a new class and metaclass.
     * 
     * @param superclass The class to use as the new class's superclass, or \c Nil to create a new root class.
     * @param name The string to use as the new class's name. The string will be copied.
     * @param extraBytes The number of bytes to allocate for indexed ivars at the end of 
     *  the class and metaclass objects. This should usually be \c 0.
     * 
     * @return The new class, or Nil if the class could not be created (for example, the desired name is already in use).
     * 
     * @note You can get a pointer to the new metaclass by calling \c object_getClass(newClass).
     * @note To create a new class, start by calling \c objc_allocateClassPair. 
     *  Then set the class's attributes with functions like \c class_addMethod and \c class_addIvar.
     *  When you are done building the class, call \c objc_registerClassPair. The new class is now ready for use.
     * @note Instance methods and instance variables should be added to the class itself. 
     *  Class methods should be added to the metaclass.
     */
    功能:创建一个新的类和这个元类
    去创建一个新的类开始通过objc_allocateClassPair,然后设置类的属性和功能用class_addMethod和class_addIvar,实例化方法和实例化变量应该被添加到类本身,类应该添加到元类中。
    OBJC_EXPORT Class _Nullable
    objc_allocateClassPair(Class _Nullable superclass, const char * _Nonnull name, 
                           size_t extraBytes) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    objc_registerClassPair

    /** 
     * Registers a class that was allocated using \c objc_allocateClassPair.
     * 
     * @param cls The class you want to register.
     */
    // 注册创建的类
    OBJC_EXPORT void
    objc_registerClassPair(Class _Nonnull cls) 
        OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    相关文章

      网友评论

          本文标题:Runtime 源码剖析

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