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 ...
随机推荐
- aSmack连接server异常smack.SmackException$ ConnectionException thrown by XMPPConnection.connect();
以下是我在研究asmack4.0出现的异常 06-17 12:02:56.924: W/System.err(10622): org.jivesoftware.smack.SmackException ...
- postgresql 导出建表语句的方法-类似describe table
https://www.youtube.com/watch?v=PMfcsYzj-9M 这个视频不错, The Definitive Guide to Object-Oriented JavaScr ...
- 关于Memcached的CAS和Set方法造成Socket泄漏的问题
为了解决多并发下写Memcached的冲突方案,我们项目组引入了CAS机制.类同于Java并发包中的CAS(Compareand set)原子操作,用来处理同一个Item被多个线程更改的并发问题.Me ...
- (六)Unity5.0新特性------新动画功能
unity 5.0 中的新动画功能 这里是你能够期待的新动画功能高速概述 ! State Machine Behaviours状态机行为 在Unity 5 中,你会能够将StateMachine ...
- Expression Tree 学习笔记(一)
大家可能都知道Expression Tree是.NET 3.5引入的新增功能.不少朋友们已经听说过这一特性,但还没来得及了解.看看博客园里的老赵等诸多牛人,将Expression Tree玩得眼花缭乱 ...
- Android 关于BottomDialogSheet 与Layout擦出爱的火花?
今天上班做那个相似于ios拍照的那种效果图 就是个垂直布局然后里面textview+切割线+textview+button 当然也能够用button+切割线+button 方法有非常多,选择适合自己的 ...
- TCP客户服务端
创建TCP服务端1.创建一个ServerSocket对象.2.调用accept()方法接收客户端请求.3.从Socket中获取I/O流.4.对I/O流进行读写操作,完成与客户端的交互.5.关闭I/O流 ...
- 使用delphi 开发多层应用(十六)使用XMLRPC 实现basic4android 远程调用RTC服务(讲述了RTC的特点,其底层通讯协议是自己封装SOCK 库,与kbmmw 的适合场合不完全一样)
RealThinClient (以下简称RTC) 也是一款delphi 多层开发的框架,由于其底层通讯协议是自己封装SOCK 库,抛弃了 大家诟病的indy,因此表现的非常稳定,效率也非常高, ...
- bootstrap中的less
一.如何加入变量 引入你的 .less 样式文件的时候要设置 rel 属性值为 “stylesheet/less”: 参考网站:http://www.bootcss.com/p/lesscss/ 1 ...
- 鼠标滑过TAB选项卡切换demo 可拓展
<html> <head> <script type="text/javascript"> <!-- function ShowTabs( ...