1. #import <Foundation/Foundation.h>
  2.  
  3. @interface CustomClass : NSObject
  4.  
  5. -(void)fun1;
  6.  
  7. @end
  8.  
  9. @interface CustomOtherClass : NSObject
  10.  
  11. -(void)fun2;
  12.  
  13. @end
  1. #import "TestClass.h"
  2. #import <objc/runtime.h>
  3. @implementation TestClass
  4.  
  5. + (BOOL)resolveInstanceMethod:(SEL)sel {
  6. Method exchangeM = class_getInstanceMethod([self class], @selector(eatWithPersonName:));
  7. //拿到IMP指针
  8. class_addMethod([self class], sel, class_getMethodImplementation(self, @selector(eatWithPersonName:)),method_getTypeEncoding(exchangeM));
  9. return YES;
  10. }
  11. - (void)eatWithPersonName:(NSString *)name {
  12. NSLog(@"Person %@ start eat ",name);
  13. }
  14. @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. 用C++实现约瑟夫环的问题

    约瑟夫问题是个有名的问题:N个人围成一圈.从第一个開始报数,第M个将被杀掉,最后剩下一个,其余人都将被杀掉. 比如N=6,M=5.被杀掉的人的序号为5,4,6.2.3.最后剩下1号. 假定在圈子里前K ...

  2. Python学习系列之logging模块

    实例一:日志写进一个文件 代码: import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filen ...

  3. stream_context_create()模拟POST/GET

    有时候,我们需要在服务器端模拟 POST/GET 等请求,也就是在 PHP 程序中去实现模拟,该怎么做到呢?或者说,在 PHP 程序里,给你一个数组,如何将这个数组 POST/GET 到另外一个地址呢 ...

  4. 通过/proc/cpuinfo判断CPU数量、Multicores、Multithreading、Hyper-threading

    http://blog.sina.com.cn/s/blog_4a6151550100iowl.html 判断依据:1.具有相同core id的cpu是同一个core的超线程.2.具有相同physic ...

  5. Jupyter notebook 使用Turorial

    The cell magics in IPython http://nbviewer.jupyter.org/github/ipython/ipython/blob/1.x/examples/note ...

  6. CAShapeLayer的使用

    CAShapeLayer的使用 1.CAShapeLayer 简介 1.CAShapeLayer继承至CALayer,可以使用CALayer的所有属性值 2.CAShapeLayer需要与贝塞尔曲线配 ...

  7. jquery 深入学习笔记之中的一个 (事件绑定)

    [jquery 事件绑定] 1.加入元素事件绑定 (1) 加入事件为当前元素 $('p').on('click',function(){ //code here ... }); (2) 加入事件为未来 ...

  8. Sharepoint2013 列表的NewForm 页面加入一个 保存新建 button

    昨天一同事问我怎样在sharepoint2013的NewForm.aspx页面上加入一个 save and new的button.实现save 和new的功能.save的功能和默认的save按钮效果一 ...

  9. MIPS 指令集将在近期开源,RISC-V 阵营慌吗?

    消息称,MIPS 指令集即将开源. eetimes 17 日报导,Wave Computing 公司表示,在明年第一季度发布最新 MIPS 指令集体系和 MIPS 最新内核 R6 的时候将开源 MIP ...

  10. 鼠标滑过TAB选项卡切换demo 可拓展

    <html> <head> <script type="text/javascript"> <!-- function ShowTabs( ...