#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. [HTML5] Show Different Variations of Images Depending on the Viewport Width using Art Direction

    For small viewports, we may want to show a variation of the desktop image. A very common use case of ...

  2. Hadoop教程(一)

    英文原文:cloudera,编译:ImportNew – Royce Wong Hadoop从这里开始!和我一起学习下使用Hadoop的基本知识,下文将以Hadoop Tutorial为主体带大家走一 ...

  3. hibernate载入持久化对象的两种方式——get、load

    一.get与load对照 在hibernate中get和load方法是依据id取得持久化对象的两种方法.但在实际使用的过程中总会把两者混淆,不知道什么情况下使用get好,什么时候使用load方法效率更 ...

  4. poj 3105 Expectation 按位统计

    题意: 给n,求sum(i^j)/(n^2),0<=i,j<n.n<10^9 分析: 暴力n^2算法肯定超时.这是logn按位统计算法:按位先算出0出现的个数x,则1出现的个数为n- ...

  5. python的pexpect模块

    Pexpect 是 Don Libes 的 Expect 语言的一个 Python 实现,是一个用来启动子程序,并使用正则表达式对程序输出做出特定响应,以此实现与其自动交互的 Python 模块. P ...

  6. [Pyhton]weakref 弱引用

    文档中的解释: https://docs.python.org/2/library/weakref.html wiki 中的解释: 在计算机程序设计中,弱引用.与强引用相对.是指不能确保其引用的对象不 ...

  7. vs2010 msvcr100.DLL 丢失!!! 用release 就可以了

  8. 在线安装Ganglia3.6.0,nginx+php搭建gweb,绝对通过

    环境:CentOS6.5 minimal 目标:安装Ganglia核心组件(gmond, gmetad, gmetric, gstat, libganglia).Ganglia web 准备 yum增 ...

  9. 在VS2010中使用MySQL-转载

    下面这篇文章进过测试,确实可以.记下来,留作记录. http://blog.sina.com.cn/s/blog_782496390100qjcu.html

  10. js 原型继承和class继承

    摘自http://www.liaoxuefeng.com/ 在传统的基于Class的语言如Java.C++中,继承的本质是扩展一个已有的Class,并生成新的Subclass. 由于这类语言严格区分类 ...