Objective-C中的runtime詳解_1

Objective-C中的runtime詳解


本文介紹本身用到過的或者遇到過的runtime函數, 持續更新中...數組

首先要想使用runtime中的函數, 必須引入#import <objc/runtime.h>頭文件.函數

1.objc打頭的方法

1.1 objc_getClass()

OBJC_EXPORT Class objc_getClass(const char *name);

這個方法做用是根據類名字的字符串, 建立class進而能夠建立對象. 以下代碼所示:ui

const char *className = [@"ViewController" UTF8String];
    Class class = objc_getClass(className);
    id instance = [[class alloc] init];
    NSLog(@"%@", [instance class]);

1.2 關聯

/////////////////////////關聯策略, 枚舉類型的
typedef OBJC_ENUM(uintptr_t, objc_AssociationPolicy) {
    OBJC_ASSOCIATION_ASSIGN = 0,           /**< Specifies a weak reference to the associated object. */
    OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, /**< Specifies a strong reference to the associated object. 
                                            *   The association is not made atomically. */
    OBJC_ASSOCIATION_COPY_NONATOMIC = 3,   /**< Specifies that the associated object is copied. 
                                            *   The association is not made atomically. */
    OBJC_ASSOCIATION_RETAIN = 01401,       /**< Specifies a strong reference to the associated object.
                                            *   The association is made atomically. */
    OBJC_ASSOCIATION_COPY = 01403          /**< Specifies that the associated object is copied.
                                            *   The association is made atomically. */
};

/** 
 * 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 object, const void *key, id value, objc_AssociationPolicy policy)
    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);

/** 
 * 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 objc_getAssociatedObject(id object, const void *key)
    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);

/** 
 * 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 object)
    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);

2.class打頭的方法

2.1 class_copyPropertyList()

OBJC_EXPORT objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount);

這個方法能夠根據一個Class和int指針類型參數, 獲取參數列表和參數的個數, 返回值是objc_property_t類型的數組, 這種類型是objc_property結構體指針.this

2.2 class_getName

OBJC_EXPORT const char *class_getName(Class cls);

返回一個類的類名字, 目前的理解, 和object_getClassName做用是相同的.atom

2.3 class_respondsToSelector()

OBJC_EXPORT BOOL class_respondsToSelector(Class cls, SEL sel);

判斷一個類是否是能相應sel方法, 返回值是BOOL類型的.指針

3.object打頭的方法

3.1 object_getClassName

OBJC_EXPORT const char *object_getClassName(id obj);

返回給定對象所屬類的類名字符串(char *類型).code

4.property打頭的方法

4.1 property_getName()

OBJC_EXPORT const char *property_getName(objc_property_t property);

參數是objc_property_t, 返回值是參數對應的char *類型的字符串. 獲得參數對應的字符串以後, 能夠利用kvc進行賦值和取值.對象

5.method打頭的方法

5.1 method_exchangeImplementations()

這個方法的做用是, 交換兩個方法的實現, 交換這個過程是原子操做. 返回值是void, 參數是要交換的兩個方法, 方法類型是Method.ci

/** 
 * Exchanges the implementations of two methods.
 * 
 * @param m1 Method to exchange with second method.
 * @param m2 Method to exchange with first method.
 * 
 * @note This is an atomic version of the following:
 *  \code 
 *  IMP imp1 = method_getImplementation(m1);
 *  IMP imp2 = method_getImplementation(m2);
 *  method_setImplementation(m1, imp2);
 *  method_setImplementation(m2, imp1);
 *  \endcode
 */
 
OBJC_EXPORT void method_exchangeImplementations(Method m1, Method m2) ;

5.2 class_getInstanceMethod()

根據Class和SEL, 獲取一個實例對象的Method, 返回值是Method.rem

//若是本對象沒有這個SEL方法, class_getInstanceMethod這個方法會去查找superclass是否包含這個方法, 可是class_copyMethodList, 不會去父類中查找.
OBJC_EXPORT Method class_getInstanceMethod(Class cls, SEL name);

5.3 class_getClassMethod()

class_getClassMethod這個方法用來獲取類方法, 返回值是Method; class_getInstanceMethod這個方法用來獲取實例方法, 返回值是Method.

OBJC_EXPORT Method class_getClassMethod(Class cls, SEL name)
相關文章
相關標籤/搜索