#import <objc/runtime.h>
#import "HaoClass.h"
#import "TwoClass.h"
#import "NSString+Hao.h"
@interface CustomClas : NSObject
{
}
-(void)objectrelease;
@end
@implementation CustomClas
int cfunction (id self,SEL _cmd,NSString * str1,NSString * str2){
NSLog(@"str1----%@,str2----%@",str1,str2);
return 99;
}
- (void)dealloc
{
NSLog(@"对象已销毁");
[super dealloc];
}
-(void)objectrelease{
HaoClas * cus=[HaoClas new];
_Block_object_dispose((__bridge const void *)(cus),0);
// [cus release];
NSLog(@"%lu",(unsigned long)cus.retainCount);
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
//1. 对象的释放:
CustomClas * cus=[CustomClas new];
//class_addMethod(<#Class cls#>, <#SEL name#>, <#IMP imp#>, <#const char *types#>);
/**
Class cls ---是为那个类:
SEL name ---方法的名字
IMP imp ----有些类似函数的指针,实现这个函数所做的事情:cfunction(id self,SEL _cmd,参数的类型1,参数的类型2..);
const char * types:---- 参数:表示为:---》 “i@:@:”i表示返回值为int 类型的, @:参数id(self):: SEL(_cmd) @:id(str)
*/
class_addMethod([CustomClas class], @selector(ocmethod::), (IMP)cfunction, "i@:@:@");
if ([cus respondsToSelector:@selector(ocmethod::)]) {
NSLog(@"ok----");
}else{
NSLog(@"失败");
}
int a=(int)[cus ocmethod:@"我的":@"你懂的"];
NSLog(@"a-----%d",a);
int b=(int)[cus ocmethod:@"我222222是oc的方法":@"你懂的"];
// class_addMethod([CustomClas class], @selector(ocMethod:), (IMP)cfunction, "i@:@");
// if ([cus respondsToSelector:@selector(ocMethod:)]) {
// NSLog(@"是的---响应成功");
// }else{NSLog(@"失败");}
// int a=(int)[cus ocMethod:@"我是oc添加的方法"];
// NSLog(@"A======%d",a);
//_Block_object_dispose((__bridge const void *)(cus),0);
//runtime可以实现对象的释放: 上面就是
//NSLog(@"%lu",(unsigned long)cus.retainCount);
//[cus release];
// NSLog(@"%lu",(unsigned long)cus.retainCount);
// id objtest =(__bridge)<#expression#>)objc_copy(obj,sizeof(obj));
//copyObj;
//3。把一个对象更改为其他类的对象
CustomClass *obj=[CustomClass new];
NSLog(@"obj----开始创建的时候------%p",obj);
// [obj fun1];
Class aClass=object_setClass(obj, [NSString class]);
//obj的类已被更改为 twoClass类啦
//NSLog(@"aClass---------:%@",NSStringFromClass(aClass));
//NSLog(@"obj Class------:%@",NSStringFromClass([obj class]));
NSLog(@"obj更改过后的时候------%p",obj);//地址还是同一块 只是指向变成了 更改的对象类分配的内存地址啦
//[obj fun1];
// [[aClass new] fun2];
TwoClass * two=[aClass new];
NSLog(@"two----开始创建的时候------%p",two);
[two fun1];
//[obj fun2];
// [obj fun1];
TwoClass *twoc=[TwoClass new];
Class ac=object_setClass(twoc, [NSString class]);
[twoc add];
// [twoc objectrelease];
//isa 指针发生了变化:
//3.2 获取对象的类是什么?
Class getclass=object_getClass(@"NSString");
NSLog(@"getclass----%@",NSStringFromClass(getclass));//twoc 在上面的时候object_setclass的时候,变成了nsstring 类的对象:
[[getclass new]add];
//5---给一个类添加方法:
//
// NSString * str=[NSString stringWithFormat:@"hhh"];
NSString *str=[[NSString alloc]init];
NSLog(@"str----开始创建的时候------%p",str);
class_addMethod([NSString class], @selector(ocN::), (IMP)cfunction ,"i@:@:@" );
if ([str respondsToSelector:@selector(ocN::)]) {
NSLog(@"NSString类方法添加成功");
}else{NSLog(@"失败");}
int c=(int)[str ocN:@"HAO":@"NSString新加的方法"];
NSLog(@"str----结束创建的时候------%p",str);
// int ccstr=(int)[str ocN:@"woshinsstring",@"NSString的方法"];
printf("nsstring----------%d\n",c);
}
return 0;
}
输出:
2015-10-03 00:19:12.859047 CF-runtime的使用[6279:6331381] ok----
2015-10-03 00:19:12.860416 CF-runtime的使用[6279:6331381] str1----我的,str2----你懂的
2015-10-03 00:19:12.860464 CF-runtime的使用[6279:6331381] a-----99
2015-10-03 00:19:12.860508 CF-runtime的使用[6279:6331381] str1----我222222是oc的方法,str2----你懂的
2015-10-03 00:19:12.860617 CF-runtime的使用[6279:6331381] obj----开始创建的时候------0x100207730
2015-10-03 00:19:12.860675 CF-runtime的使用[6279:6331381] obj更改过后的时候------0x100207730
2015-10-03 00:19:12.860718 CF-runtime的使用[6279:6331381] two----开始创建的时候------0x100303360
2015-10-03 00:19:12.860751 CF-runtime的使用[6279:6331381] FUN1
2015-10-03 00:19:12.860801 CF-runtime的使用[6279:6331381] nsstring + Hao 类别
2015-10-03 00:19:12.860918 CF-runtime的使用[6279:6331381] getclass----__NSCFConstantString
2015-10-03 00:19:12.860964 CF-runtime的使用[6279:6331381] nsstring + Hao 类别
2015-10-03 00:19:12.861077 CF-runtime的使用[6279:6331381] str----开始创建的时候------0x7fffb047a9a0
2015-10-03 00:19:12.861179 CF-runtime的使用[6279:6331381] NSString类方法添加成功
2015-10-03 00:19:12.861245 CF-runtime的使用[6279:6331381] str1----HAO,str2----NSString新加的方法
2015-10-03 00:19:12.861291 CF-runtime的使用[6279:6331381] str----结束创建的时候------0x7fffb047a9a0
nsstring----------99
Program ended with exit code: 0