序言
本篇文章主要介紹runtime有關(guān)API及使用
一 類
1.1 動(dòng)態(tài)創(chuàng)建一個(gè)類(參數(shù):父類渣慕,類名,額外的內(nèi)存空間)
Class objc_allocateClassPair(Class superclass, const char *name, size_t extraBytes)
1.2 注冊(cè)一個(gè)類(要在類注冊(cè)之前添加成員變量)
void objc_registerClassPair(Class cls)
1.3 銷毀一個(gè)類
void objc_disposeClassPair(Class cls)
- 以上三個(gè)方法使用下面例子做講解
// 建一個(gè)命令行工程
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
void run(id self, SEL _cmd) {
NSLog(@"_____ %@ - %@", self, NSStringFromSelector(_cmd));
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
// 動(dòng)態(tài)創(chuàng)建一個(gè)類
Class newClass = objc_allocateClassPair([NSObject class], "CSDog", 0);
// 添加屬性
class_addIvar(newClass, "_age", 4, 1, @encode(int));
class_addIvar(newClass, "_weight", 4, 1, @encode(int));
// 添加方法
class_addMethod(newClass, @selector(run), (IMP)run, "v@:");
// 注冊(cè)類
objc_registerClassPair(newClass);
id dog = [[newClass alloc] init];
[dog setValue:@10 forKey:@"_age"];
[dog setValue:@20 forKey:@"_weight"];
[dog run];
NSLog(@"%@ %@", [dog valueForKey:@"_age"], [dog valueForKey:@"_weight"]);
// 在不需要這個(gè)類時(shí)釋放
objc_disposeClassPair(newClass);
}
return 0;
}
運(yùn)行結(jié)果
1.4 獲取isa指向的Class
Class object_getClass(id obj)
1.5 設(shè)置isa指向的Class
Class object_setClass(id obj, Class cls)
1.6 判斷一個(gè)OC對(duì)象是否為Class
BOOL object_isClass(id obj)
- 上面三個(gè)方法以下面代碼做例子
@interface CSPersion : NSObject
- (void)run;
@end
@implementation CSPersion
- (void)run {
NSLog(@"%s", __func__);
}
@end
@interface CSCar : NSObject
- (void)run;
@end
@implementation CSCar
- (void)run {
NSLog(@"%s", __func__);
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
CSPersion *persion = [[CSPersion alloc] init];
[persion run];
object_setClass(persion, [CSCar class]);
[persion run];
object_getClass([CSPersion class]);
NSLog(@"%p %p", object_getClass([CSPersion class]), [CSPersion class]);
NSLog(@"%d %d %d",
object_isClass(persion),
object_isClass([CSPersion class]),
object_isClass(object_getClass([CSPersion class]))
);
}
return 0;
}
運(yùn)行結(jié)果如下
1.7 判斷一個(gè)Class是否為元類
BOOL class_isMetaClass(Class cls)
1.8 獲取父類
Class class_getSuperclass(Class cls)
二 成員變量
公共使用類
@interface CSPersion : NSObject
@property (assign, nonatomic) int ID;
@property (assign, nonatomic) int weight;
@property (assign, nonatomic) int age;
@property (copy, nonatomic) NSString *name;
- (void)run;
@end
2.1 獲取一個(gè)實(shí)例變量信息
Ivar class_getInstanceVariable(Class cls, const char *name)
2.2 獲取成員變量的相關(guān)信息
const char *ivar_getName(Ivar v)
const char *ivar_getTypeEncoding(Ivar v)
2.1,2.2使用如下代碼做例子
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
// 1.獲取成員變量信息
Ivar ageIvar = class_getInstanceVariable([CSPersion class], "_age");
NSLog(@"%s %s",ivar_getName(ageIvar),ivar_getTypeEncoding(ageIvar));
}
return 0;
}
運(yùn)行結(jié)果
2.3 設(shè)置和獲取成員變量的值
void object_setIvar(id obj, Ivar ivar, id value)
id object_getIvar(id obj, Ivar ivar)
代碼例子如下
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 2\. 設(shè)置和獲取成員變量的值
CSPersion *persion = [[CSPersion alloc] init];
Ivar nameIvar = class_getInstanceVariable([CSPersion class], "_name");
Ivar ageIvar = class_getInstanceVariable([CSPersion class], "_age");
object_setIvar(persion, nameIvar, @"123");
object_setIvar(persion, ageIvar, (__bridge id)(void *)10);
NSLog(@"%@ %d", persion.name, persion.age);
}
return 0;
}
運(yùn)行結(jié)果如下
2.4 動(dòng)態(tài)添加成員變量(已經(jīng)注冊(cè)的類是不能動(dòng)態(tài)添加成員變量的)
BOOL class_addIvar(Class cls, const char * name, size_t size, uint8_t alignment, const char * types)
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 動(dòng)態(tài)創(chuàng)建一個(gè)類
Class newClass = objc_allocateClassPair([NSObject class], "CSDog", 0);
// 添加屬性
class_addIvar(newClass, "_age", 4, 1, @encode(int));
class_addIvar(newClass, "_weight", 4, 1, @encode(int));
// 添加方法
class_addMethod(newClass, @selector(run), (IMP)run, "v@:");
// 注冊(cè)類
objc_registerClassPair(newClass);
id dog = [[newClass alloc] init];
[dog setValue:@10 forKey:@"_age"];
[dog setValue:@20 forKey:@"_weight"];
[dog run];
NSLog(@"%@ %@", [dog valueForKey:@"_age"], [dog valueForKey:@"_weight"]);
// 在不需要這個(gè)類時(shí)釋放
objc_disposeClassPair(newClass);
}
return 0;
}
運(yùn)行結(jié)果如下
2.5 拷貝實(shí)例變量列表(最后需要調(diào)用free釋放)
Ivar *class_copyIvarList(Class cls, unsigned int *outCount)
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 4.成員變量的數(shù)量
unsigned int count;
Ivar *ivars = class_copyIvarList([CSPersion class], &count);
for (int i = 0; i < count; i++) {
// 取出i位置的成員變量
Ivar ivar = ivars[I];
NSLog(@"%s %s", ivar_getName(ivar), ivar_getTypeEncoding(ivar));
}
free(ivars);
}
return 0;
}
運(yùn)行結(jié)果如下
三 屬性
3.1 獲取一個(gè)屬性
objc_property_t class_getProperty(Class cls, const char *name)
3.2 拷貝屬性列表(最后需要調(diào)用free釋放)
objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount)
3.3 動(dòng)態(tài)添加屬性
BOOL class_addProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount)
3.4 動(dòng)態(tài)替換屬性
void class_replaceProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount)
3.5 獲取屬性的一些信息
const char *property_getName(objc_property_t property)
const char *property_getAttributes(objc_property_t property)
四 方法
4.1 獲得一個(gè)實(shí)例方法鸡岗、類方法
Method class_getInstanceMethod(Class cls, SEL name)
Method class_getClassMethod(Class cls, SEL name)
4.2 方法實(shí)現(xiàn)相關(guān)操作
IMP class_getMethodImplementation(Class cls, SEL name)
IMP method_setImplementation(Method m, IMP imp)
void method_exchangeImplementations(Method m1, Method m2)
4.3 拷貝方法列表(最后需要調(diào)用free釋放)
Method *class_copyMethodList(Class cls, unsigned int *outCount)
4.4 動(dòng)態(tài)添加方法
BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)
4.5 動(dòng)態(tài)替換方法
IMP class_replaceMethod(Class cls, SEL name, IMP imp, const char *types)
4.6 獲取方法的相關(guān)信息(帶有copy的需要調(diào)用free去釋放)
SEL method_getName(Method m)
IMP method_getImplementation(Method m)
const char *method_getTypeEncoding(Method m)
unsigned int method_getNumberOfArguments(Method m)
char *method_copyReturnType(Method m)
char *method_copyArgumentType(Method m, unsigned int index)
4.7 選擇器相關(guān)
const char *sel_getName(SEL sel)
SEL sel_registerName(const char *str)
4.8 用block作為方法實(shí)現(xiàn)
IMP imp_implementationWithBlock(id block)
id imp_getBlock(IMP anImp)
BOOL imp_removeBlock(IMP anImp)
本文參考:
路飛_Luck (http://www.reibang.com/p/07f7b96bb03f)
以及借鑒MJ的教程視頻
非常感謝.
項(xiàng)目連接地址-runtime_API
項(xiàng)目連接地址-runtime_exchangeMethod