IOS高级开发~Runtime(一)
#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(一)的更多相关文章
- IOS高级开发 runtime(一)
一. 简介 IOS 开发中灵活使用runtime 会提高我们的程序性能和开发速度.要想使用runtime,首先要引入系统的头文件. <span style="font-size:18p ...
- iOS 高级开发 runtime(三)
三 .动态添加方法 我们可以通过runtime动态地添加方法.那么到底啥叫动态添加方法呢?动态添加方法就是当我们程序运行时才知道我们应该调用哪个方法.我们首先需要了解这一点,当我们编写完一段代码后,我 ...
- IOS 高级开发 runtime(二)
二.移魂大法 使用runtime还可以交换两个函数.先贴上代码和执行结果. #import <Foundation/Foundation.h> @interface DZLPerson : ...
- (转发)IOS高级开发~Runtime(四)
用C代替OC: #import <objc/runtime.h> #import <objc/message.h> #import <stdio.h> extern ...
- (转发)IOS高级开发~Runtime(三)
11.系统类的方法实现部分替换 - (void) methodExchange { Method m1 = class_getInstanceMethod([NSStringclass],@selec ...
- (转发)IOS高级开发~Runtime(二)
一些公用类: @interface ClassCustomClass :NSObject{ NSString *varTest1; NSString *varTest2; NSString *varT ...
- (转发)IOS高级开发~Runtime(一)
IOS高级开发-Runtime(一) IOS高级开发-Runtime(二) IOS高级开发-Runtime(三) IOS高级开发-Runtime(四) 一些公用类: @interface Custom ...
- IOS高级开发之多线程(四)NSOperation
1.什么是NSOperation,NSOperationQueue? NSOperation是一个抽象的基类,表示一个独立的计算单元,可以为子类提供有用且线程安全的建立状态,优先级,依赖和取消等操作. ...
- IOS高级开发~Runtime(二)
#import <Foundation/Foundation.h> @interface CustomClass : NSObject { NSString *varTest1; NSSt ...
随机推荐
- gameplay理解
Camera视角:确定显示的视场及视角. Game:显示的基类.静态单例模式.但是获取方式很奇怪. Game::getInstance得到的是__gameInstance,但是__gameInstan ...
- linux下ndk编译命令行程序及配置
1.在http://developer.android.com/tools/sdk/ndk/index.html下载Android-ndk-r8e-linux-x86.tar.bz2,解压后把andr ...
- Cocoapods Undefined symbols for architecture armv7s\arm64
此类错误 "_OBJC_CLASS_$_AFURLSessionManager", referenced from: 解决的方法 在other linker flags里加入一行 ...
- 疯狂Java学习笔记(77)-----------凝视注意事项
代码凝视,能够说是比代码本身更重要.这里有一些方法能够确保你写在代码中的凝视是友好的: 不要反复阅读者已经知道的内容 能明白说明代码是做什么的凝视对我们是没有帮助的. // If the color ...
- Redis 脚本及其应用
参考:http://www.runoob.com/redis/redis-scripting.html Redis 脚本使用 Lua 解释器来执行脚本. Reids 2.6 版本通过内嵌支持 Lua ...
- JAVA学习第六十五课 — 正則表達式
正則表達式:主要应用于操作字符串.通过一些特定的符号来体现 举例: QQ号的校验 6~9位.0不得开头.必须是数字 String类中有matches方法 matches(String regex) 告 ...
- WPF绑定各种数据源之object数据源
一.WPF绑定各种数据源索引 WPF 绑定各种数据源之Datatable WPF绑定各种数据源之object数据源 WPF绑定各种数据源之xml数据源 WPF绑定各种数据源之元素控件属性 Bindin ...
- Serializable 序列化 The byte stream created is platform independent. So, the object serialized on one platform can be deserialized on a different platform.
Java 序列化接口Serializable详解 - 火星猿类 - 博客园 http://www.cnblogs.com/tomtiantao/p/6866083.html 深入理解JAVA序列化 - ...
- Deep Learning 32: 自己写的keras的一个callbacks函数,解决keras中不能在每个epoch实时显示学习速率learning rate的问题
一.问题: keras中不能在每个epoch实时显示学习速率learning rate,从而方便调试,实际上也是为了调试解决这个问题:Deep Learning 31: 不同版本的keras,对同样的 ...
- 解析腾讯企业邮箱到自己域名,设置mail的cname
之前注册了腾讯企业邮的免费邮箱,后来想把企业邮箱和域名绑定起来,发现了一些问题. 先来看正常的部分,假设你已经注册过了腾讯企业邮箱免费版,并且已经绑定好了域名. 然后在域名提供商那里设置域名解析的MX ...