成員變量 | 類(lèi)相關(guān)
person *perso = [[person alloc]init];
//獲取元類(lèi)or類(lèi)
NSLog(@"%p---%p", object_getClass([person class]),[perso class]);
//設(shè)置isa指向的class
object_setClass(perso,[cat class]);
[perso performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:YES];
//生成對(duì)象 添加成員 添加方法 賦值成員
Class newClass = objc_allocateClassPair([NSObject class], "Dog", 0);
//方法任何時(shí)候可以添加
class_addMethod(newClass, @selector(run), (IMP)run, "v@:");
//在注冊(cè)之前 已存在的不能添加
class_addIvar(newClass, "_age2", 4, 1, @encode(int));
//注冊(cè)類(lèi)
objc_registerClassPair(newClass);
id dyClass = [newClass new];
[dyClass setValue:@20 forKey:@"_age2"];
//不需要使用的時(shí)候 釋放
// objc_disposeClassPair(newClass);
//獲取成員變量
person *perso2 = [[person alloc]init];
Ivar nameIvar = class_getInstanceVariable([perso2 class], "_name");
Ivar ageIvar = class_getInstanceVariable([perso2 class], "_age");
NSLog(@"%s--- %s",ivar_getName(ageIvar),ivar_getTypeEncoding(ageIvar));
//設(shè)置和獲取成員變量的值 最后一個(gè)參數(shù)非空對(duì)象
object_setIvar(perso2, nameIvar, @"12345");
object_setIvar(perso2, ageIvar, (__bridge id _Nullable)((void *)10));
NSLog(@"----%@---%d",perso2.name,perso2.age);
//成員變量的數(shù)量
unsigned int cout;
Ivar *ivars = class_copyIvarList([perso2 class], &cout);
/*
1, 這里我們可以拿到某些控件的私有屬性,來(lái)進(jìn)行一些操作
如利用kvc ,給textfield的placehold屬性進(jìn)行賦值.
2, 字典轉(zhuǎn)模型
*/
for (int i = 0; i < cout; i++) {
Ivar ivar = ivars[i];
NSLog(@"%s",ivar_getName(ivar));
}
方法相關(guān)
void running(id self,SEL _cmd){
NSLog(@"running");
}
cat *c1 = [[cat alloc]init];
// class_replaceMethod([c1 class], @selector(run), (IMP)run2, "v@:");
//方法替換
class_replaceMethod([c1 class], @selector(run), imp_implementationWithBlock(^{
NSLog(@"block");
}), "v");
[c1 run];
Method thod1 = class_getInstanceMethod([c1 class], @selector(run));
Method thod2 = class_getInstanceMethod([c1 class], @selector(otherRun));
//方法交換
method_exchangeImplementations(thod1, thod2);
[c1 run];
//遍歷方法
unsigned int count ;
Method *methods = class_copyMethodList(c1.class, &count);
for (int i = 0 ; i<count; i++) {
Method method = methods[i];
NSLog(@"%@---",NSStringFromSelector(method_getName(method)));
}
//添加方法
class_addMethod(c1.class, @selector(running), (IMP)running, "v@:");
[c1 performSelectorOnMainThread:@selector(running) withObject:nil waitUntilDone:YES];
實(shí)際應(yīng)用:如防止button重復(fù)點(diǎn)擊
+ (void)load{
Method thod1 = class_getInstanceMethod([self class], @selector(sendAction:to:forEvent:));
Method thod2 = class_getInstanceMethod([self class], @selector(My_sendAction:to:forEvent:));
method_exchangeImplementations(thod1, thod2);
}
- (void)My_sendAction:(SEL)action to:(nullable id)target forEvent:(nullable UIEvent *)event{
if (self.avoidDoubleCick == NO) {
self.avoidDoubleCick = YES;
[self My_sendAction:action to:target forEvent:event];
[self performSelector:@selector(setAvoidDoubleCick:) withObject:@(NO) afterDelay:2];
}
}
- (void)setAvoidDoubleCick:(BOOL)avoidDoubleCick{
objc_setAssociatedObject(self, @selector(avoidDoubleCick), @(avoidDoubleCick), OBJC_ASSOCIATION_ASSIGN);
}
- (BOOL)avoidDoubleCick{
return [objc_getAssociatedObject(self, @selector(avoidDoubleCick))boolValue];
}
數(shù)組防止加入空對(duì)象
@implementation NSMutableArray (extension)
+(void)load{
Class cls = NSClassFromString(@"__NSArrayM");
Method method1 = class_getInstanceMethod(cls, @selector(insertObject:atIndex:));
Method method2 = class_getInstanceMethod(cls, @selector(my_insertObject:atIndex:));
method_exchangeImplementations(method1, method2);
}
- (void)my_insertObject:(id)anObject atIndex:(NSUInteger)index{
if(anObject== nil)return;
[self my_insertObject:anObject atIndex:index];
}
@end
-
Foundation
框架有一些類(lèi)型實(shí)質(zhì)上和看到的不一樣(類(lèi)簇),可以輸出class
看出其真實(shí)類(lèi)型從而進(jìn)行方法交換.
- 方法交換實(shí)質(zhì)上
IMP
的交換,并且會(huì)清空之前的緩存.