查看私有成员变量
- 获取到成员变量的名称,就能通过KVC为私有成员变量设置值,比如
unsigned int count;
Ivar *ivars = class_copyIvarList(self, &count);
for (int i = 0; i < count; i++) {
// 取出i位置的成员变量
Ivar ivar = ivars[i];
NSMutableString *name = [NSMutableString stringWithUTF8String:ivar_getName(ivar)];
}
free(ivars);
替换方法实现,交换方法
class_replaceMethod
mehod_exchangeImplementations
- 最常见的应用场景就是在分类中,替换本类中的方法实现,添加一些额外的操作
+ (void)load
{
// 加dispatch_once的目的是为了只调用一次,因为外部可以手动调用+load方法,虽然很另类
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class cls = NSClassFromString(@"__NSDictionaryM");
Method method1 = class_getInstanceMethod(cls, @selector(setObject:forKeyedSubscript:));
Method method2 = class_getInstanceMethod(cls, @selector(mj_setObject:forKeyedSubscript:));
method_exchangeImplementations(method1, method2);
Class cls2 = NSClassFromString(@"__NSDictionaryI");
Method method3 = class_getInstanceMethod(cls2, @selector(objectForKeyedSubscript:));
Method method4 = class_getInstanceMethod(cls2, @selector(mj_objectForKeyedSubscript:));
method_exchangeImplementations(method3, method4);
});
}
- (void)mj_setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key
{
if (!key) return;
[self mj_setObject:obj forKeyedSubscript:key];
}
- (id)mj_objectForKeyedSubscript:(id)key
{
if (!key) return nil;
return [self mj_objectForKeyedSubscript:key];
}
Runtime类相关的API
- 动态创建一个类(参数:父类,类名,额外的内存空间)
Class objc_allocateClassPair(Class superclass, const char *name, size_t extraBytes)
void objc_registerClassPair(Class cls)
void objc_disposeClassPair(Class cls)
Class object_getClass(id obj)
Class object_setClass(id obj, Class cls)
BOOL object_isClass(id obj)
BOOL class_isMetaClass(Class cls)
Class class_getSuperclass(Class cls)
Runtime成员变量相关API
Ivar class_getInstanceVariable(Class cls, const char *name)
Ivar *class_copyIvarList(Class cls, unsigned int *outCount)
void object_setIvar(id obj, Ivar ivar, id value)
id object_getIvar(id obj, Ivar ivar)
- 动态添加成员变量(已经注册的类是不能动态添加成员变量的)
BOOL class_addIvar(Class cls, const char * name, size_t size, uint8_t alignment, const char * types)
const char *ivar_getName(Ivar v)
const char *ivar_getTypeEncoding(Ivar v)
Runtime属性相关的API
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)
const char *property_getName(objc_property_t property)
const char *property_getAttributes(objc_property_t property)
Runtime与方法相关的API
Method class_getInstanceMethod(Class cls, SEL name)
Method class_getClassMethod(Class cls, SEL name)
IMP class_getMethodImplementation(Class cls, SEL name)
IMP method_setImplementation(Method m, IMP imp)
void method_exchangeImplementations(Method m1, Method m2)
Method *class_copyMethodList(Class cls, unsigned int *outCount)
BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)
IMP class_replaceMethod(Class cls, SEL name, IMP imp, const char *types)
- 获取方法的相关信息(带有copy的需要调用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)
const char *sel_getName(SEL sel)
SEL sel_registerName(const char *str)
IMP imp_implementationWithBlock(id block)
id imp_getBlock(IMP anImp)
BOOL imp_removeBlock(IMP anImp)
网友评论