#import <Foundation/Foundation.h>

@interface CustomClass : NSObject

-(void)fun1;

@end

@interface CustomOtherClass : NSObject

-(void)fun2;

@end
#import "TestClass.h"
#import <objc/runtime.h>
@implementation TestClass + (BOOL)resolveInstanceMethod:(SEL)sel {
Method exchangeM = class_getInstanceMethod([self class], @selector(eatWithPersonName:));
//拿到IMP指针
class_addMethod([self class], sel, class_getMethodImplementation(self, @selector(eatWithPersonName:)),method_getTypeEncoding(exchangeM));
return YES;
}
- (void)eatWithPersonName:(NSString *)name {
NSLog(@"Person %@ start eat ",name);
}
@end

/**

对象拷贝

*/

-(void)copyObj{

CustomClass *obj = [CustomClass new];

NSLog(@"对象拷贝:%p",&obj);

//完全是一个新的对象

id objTest = object_copy(obj,sizeof(obj));

NSLog(@"%p", &objTest);

[objTest fun1];

}

/**

对象释放

*/

-(void)objectDispose{

CustomClass *obj = [CustomClass new];

(void)(NSLog(@"---%ld",obj.retainCount)),

object_dispose(obj);

//(void)(NSLog(@"---%ld",obj.retainCount)),

//[obj release];

//NSLog(@"---%ld",obj.retainCount);

//[obj fun1];

}

/**

更改对象的类

*/

-(void)setClassTest{

CustomClass *obj = [CustomClass new];

[obj fun1];

Class aclass = object_setClass(obj, [CustomOtherClass class]);

//obj 对象的类被更改了    swap the isa to an isa

NSLog(@"aClass:%@",NSStringFromClass(aclass));

NSLog(@"obj class:%@",NSStringFromClass([obj class]));

[obj fun2];

}

-(void)getClassTest{

CustomClass *obj = [CustomClass new];

Class alogClass = object_getClass(obj);

NSLog(@"--get:%@",NSStringFromClass(alogClass));

}

/**

获取对象的类名

*/

- (void) getClassName{

CustomClass *obj = [CustomClass new];

NSString *className = [NSString stringWithCString:object_getClassName(obj) encoding:NSUTF8StringEncoding];

NSLog(@"className:%@",className);

}

/**

动态给一个类添加方法

*/

- (void)oneParam{

TestClass *instance = [[TestClass alloc]init];

//方法添加

[instance performSelector:@selector(eatWithPersonName:) withObject:@"mujin"];

}

IOS高级开发~Runtime(一)的更多相关文章

  1. IOS高级开发 runtime(一)

    一. 简介 IOS 开发中灵活使用runtime 会提高我们的程序性能和开发速度.要想使用runtime,首先要引入系统的头文件. <span style="font-size:18p ...

  2. iOS 高级开发 runtime(三)

    三 .动态添加方法 我们可以通过runtime动态地添加方法.那么到底啥叫动态添加方法呢?动态添加方法就是当我们程序运行时才知道我们应该调用哪个方法.我们首先需要了解这一点,当我们编写完一段代码后,我 ...

  3. IOS 高级开发 runtime(二)

    二.移魂大法 使用runtime还可以交换两个函数.先贴上代码和执行结果. #import <Foundation/Foundation.h> @interface DZLPerson : ...

  4. (转发)IOS高级开发~Runtime(四)

    用C代替OC: #import <objc/runtime.h> #import <objc/message.h> #import <stdio.h> extern ...

  5. (转发)IOS高级开发~Runtime(三)

    11.系统类的方法实现部分替换 - (void) methodExchange { Method m1 = class_getInstanceMethod([NSStringclass],@selec ...

  6. (转发)IOS高级开发~Runtime(二)

    一些公用类: @interface ClassCustomClass :NSObject{ NSString *varTest1; NSString *varTest2; NSString *varT ...

  7. (转发)IOS高级开发~Runtime(一)

    IOS高级开发-Runtime(一) IOS高级开发-Runtime(二) IOS高级开发-Runtime(三) IOS高级开发-Runtime(四) 一些公用类: @interface Custom ...

  8. IOS高级开发之多线程(四)NSOperation

    1.什么是NSOperation,NSOperationQueue? NSOperation是一个抽象的基类,表示一个独立的计算单元,可以为子类提供有用且线程安全的建立状态,优先级,依赖和取消等操作. ...

  9. IOS高级开发~Runtime(二)

    #import <Foundation/Foundation.h> @interface CustomClass : NSObject { NSString *varTest1; NSSt ...

随机推荐

  1. iOS --- 总结Objective-C中经常使用的宏定义(持续更新中)

    将iOS开发中经常使用的宏定义整理例如以下,仅包括Objective-C. 而对于Swift,不能使用宏,则能够定义全局函数或者extension.请參考博客iOS - 总结Swift中经常使用的全局 ...

  2. 微信小程序 开发环境配置

    1.注册小程序 (1)微信公众平台:https://mp.weixin.qq.com/(2)立即注册 (3)流程 (4)小程序注册 (5)填写相关信息,并去邮箱激活.这样小程序的账号就注册完成了. 2 ...

  3. NDK编译STL

    方法: 1.在jni目录下新建Application.mk; 加入 APP_STL :=  stlport_static  右边的值还可以换成下面几个: system - 使用默认最小的C++运行库, ...

  4. font-family,font-size,color

      CreateTime--2017年12月20日16:43:35 Author:Marydon css设置字体样式 1.font-family 语法:属性值可以有一个或多个,多个值之间使用逗号隔开. ...

  5. 一个TAB的jquery简单写法

    <style> .honver{ color:red;} </style><script src="../js/jquery-1.9.0.min.js" ...

  6. LeetCode_DP_Word Break II

    LeetCode_Word Break II 一.题目描写叙述: 二.解决思路: 题目要求我们要在原字符串中加空格,使得隔开的每一个词都是词典中的词. 所以我们大能够按顺序扫描每一个字符.可是然后当碰 ...

  7. xcode4中build Settings常见参数解析

    本文转载至 http://shiminghua234.blog.163.com/blog/static/263912422012411103526386/     1.Installation Dir ...

  8. STM32唯一身份识别ID(器件电子签名)的读取以及芯片Flash大小读取

    每个STM32有一个独立的ID,这个ID可以用来: 产品唯一的身份标识的作用:    ●  用来作为序列号(例如USB字符序列号或者其他的终端应用):    ●  用来作为密码,在编写闪存时,将此唯一 ...

  9. poj3349(hash or violence)

    Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 38600   Accep ...

  10. Exchangeable image file format

    Exif - Wikipedia https://en.wikipedia.org/wiki/Exif