Objective-C Runtime 的 C 語言介面涵蓋類、實例、屬性、方法、選擇器、協議與 Block 七大面向。以下依分類整理常用函數,作為查閱與實作時的參照。
// 取得類名 const char * class_getName ( Class cls ); // 取得父類 Class class_getSuperclass ( Class cls ); // 判斷是否為元類 BOOL class_isMetaClass ( Class cls ); // 取得實例大小 size_t class_getInstanceSize ( Class cls ); // 取得指定名稱的實例成員變數 Ivar class_getInstanceVariable ( Class cls, const char *name ); // 取得類成員變數 Ivar class_getClassVariable ( Class cls, const char *name ); // 添加成員變數 BOOL class_addIvar ( Class cls, const char *name, size_t size, uint8_t alignment, const char *types ); // 取得全部成員變數列表 Ivar * class_copyIvarList ( Class cls, unsigned int *outCount ); // 屬性相關 objc_property_t class_getProperty ( Class cls, const char *name ); objc_property_t * class_copyPropertyList ( Class cls, unsigned int *outCount ); BOOL class_addProperty ( Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount ); void class_replaceProperty ( Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount ); // 方法相關 BOOL class_addMethod ( Class cls, SEL name, IMP imp, const char *types ); Method class_getInstanceMethod ( Class cls, SEL name ); Method class_getClassMethod ( Class cls, SEL name ); Method * class_copyMethodList ( Class cls, unsigned int *outCount ); IMP class_replaceMethod ( Class cls, SEL name, IMP imp, const char *types ); IMP class_getMethodImplementation ( Class cls, SEL name ); IMP class_getMethodImplementation_stret ( Class cls, SEL name ); BOOL class_respondsToSelector ( Class cls, SEL sel ); // 協議相關 BOOL class_addProtocol ( Class cls, Protocol *protocol ); BOOL class_conformsToProtocol ( Class cls, Protocol *protocol ); Protocol * class_copyProtocolList ( Class cls, unsigned int *outCount ); // 版本號 int class_getVersion ( Class cls ); void class_setVersion ( Class cls, int version );
// 拷貝與釋放 id object_copy ( id obj, size_t size ); id object_dispose ( id obj ); // 實例變數取值與設值 Ivar object_setInstanceVariable ( id obj, const char *name, void *value ); Ivar object_getInstanceVariable ( id obj, const char *name, void **outValue ); void * object_getIndexedIvars ( id obj ); id object_getIvar ( id obj, Ivar ivar ); void object_setIvar ( id obj, Ivar ivar, id value ); // 類別資訊 const char * object_getClassName ( id obj ); Class object_getClass ( id obj ); Class object_setClass ( id obj, Class cls ); // 已註冊類別的查詢 int objc_getClassList ( Class *buffer, int bufferCount ); Class * objc_copyClassList ( unsigned int *outCount ); Class objc_lookUpClass ( const char *name ); Class objc_getClass ( const char *name ); Class objc_getRequiredClass ( const char *name ); Class objc_getMetaClass ( const char *name ); // 關聯對象 void objc_setAssociatedObject ( id object, const void *key, id value, objc_AssociationPolicy policy ); id objc_getAssociatedObject ( id object, const void *key ); void objc_removeAssociatedObjects ( id object ); // 映像檔資訊 const char ** objc_copyImageNames ( unsigned int *outCount ); const char * class_getImageName ( Class cls ); const char ** objc_copyClassNamesForImage ( const char *image, unsigned int *outCount );
const char * property_getName ( objc_property_t property ); const char * property_getAttributes ( objc_property_t property ); char * property_copyAttributeValue ( objc_property_t property, const char *attributeName ); objc_property_attribute_t * property_copyAttributeList ( objc_property_t property, unsigned int *outCount );
// 直接調用實作 id method_invoke ( id receiver, Method m, ... ); void method_invoke_stret ( id receiver, Method m, ... ); // 名稱與實作 SEL method_getName ( Method m ); IMP method_getImplementation ( Method m ); IMP method_setImplementation ( Method m, IMP imp ); // 型別編碼 const char * method_getTypeEncoding ( Method m ); char * method_copyReturnType ( Method m ); char * method_copyArgumentType ( Method m, unsigned int index ); void method_getReturnType ( Method m, char *dst, size_t dst_len ); void method_getArgumentType ( Method m, unsigned int index, char *dst, size_t dst_len ); // 參數數量與描述 unsigned int method_getNumberOfArguments ( Method m ); struct objc_method_description * method_getDescription ( Method m ); // 交換兩個方法的實作(Method Swizzling 的核心) void method_exchangeImplementations ( Method m1, Method m2 );
const char * sel_getName ( SEL sel ); SEL sel_registerName ( const char *str ); SEL sel_getUid ( const char *str ); BOOL sel_isEqual ( SEL lhs, SEL rhs );
selector 在本質上就是方法名的唯一標識,系統以字串映射到 SEL,因此 sel_registerName 與 sel_getUid 都扮演著註冊與查詢的角色。
// 取得與列舉 Protocol * objc_getProtocol ( const char *name ); Protocol ** objc_copyProtocolList ( unsigned int *outCount ); // 動態建立與註冊 Protocol * objc_allocateProtocol ( const char *name ); void objc_registerProtocol ( Protocol *proto ); // 為協議添加內容 void protocol_addMethodDescription ( Protocol *proto, SEL name, const char *types, BOOL isRequiredMethod, BOOL isInstanceMethod ); void protocol_addProtocol ( Protocol *proto, Protocol *addition ); void protocol_addProperty ( Protocol *proto, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount, BOOL isRequiredProperty, BOOL isInstanceProperty ); // 查詢 const char * protocol_getName ( Protocol *p ); BOOL protocol_isEqual ( Protocol *proto, Protocol *other ); struct objc_method_description * protocol_copyMethodDescriptionList ( Protocol *p, BOOL isRequiredMethod, BOOL isInstanceMethod, unsigned int *outCount ); struct objc_method_description protocol_getMethodDescription ( Protocol *p, SEL aSel, BOOL isRequiredMethod, BOOL isInstanceMethod ); objc_property_t * protocol_copyPropertyList ( Protocol *proto, unsigned int *outCount ); objc_property_t protocol_getProperty ( Protocol *proto, const char *name, BOOL isRequiredProperty, BOOL isInstanceProperty ); Protocol ** protocol_copyProtocolList ( Protocol *proto, unsigned int *outCount ); BOOL protocol_conformsToProtocol ( Protocol *proto, Protocol *other );
// 以 Block 產生 IMP,可用於方法交換或動態實作 IMP imp_implementationWithBlock ( id block ); // 取出與 IMP 關聯的 Block id imp_getBlock ( IMP anImp ); // 解除關聯並釋放 Block 拷貝 BOOL imp_removeBlock ( IMP anImp );
這組函數在實務上非常實用:想要為無法修改原始碼的類別動態補上方法實作,就可以用 imp_implementationWithBlock 把 Block 轉成 IMP,再透過 class_addMethod 掛上去,這是除錯工具與 AOP 應用的常見手法。