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 ...
随机推荐
- chrome备份网站
chrome备份网站 https://www.chromedownloads.net/
- JAVA_MyEclipse如何加载Tomcat
注意Tomcat不要放到Program Files这种有空格的路径下面!,下图所示是错误的
- Type cannot use 'try' with exceptions disabled
cannot use ‘throw’ with exceptions disabled 在为 DragonBonesCPP/refactoring 的 cocos2d-x-3.2 demo 增加 An ...
- PHP读取excel(6)
有时候我们只需要读取某些指定sheet,具体代码如下: <?php header("Content-Type:text/html;charset=utf-8"); //引入读 ...
- Python基础二--基本控制语句
基本接触每一种语言,都须要做的:1.print 一个"Hello world!" 2.了解主要的数据类型 3.学习控制语句. 当我们学习控制语句,一般都离不开if,for ,whi ...
- RecyclerView(替代ListView)使用方法介绍
在build.gradle文件加入以下代码 compile 'com.android.support:cardview-v7:21.0.3' compile 'com.android.support: ...
- Yarn调度器负载模拟器——Yarn Scheduler Load Simulator (SLS)
一.概述: Yarn调度器有很多实现,如Fifo, Capacity和Fair schedulers等.与其同一时候,正在进行一些优化措施来提高调度器在不同负载和工作场景下的性能.每一个调度器都有自己 ...
- IE67下float左右对齐
例子: <style> .h1{text-align: left;} .leftA{color: #000} .rightA{color: #ccc; float: right;} < ...
- ios实现倒计时的两种方法
方法1:使用NSTimer来实现 主要使用的是NSTimer的scheduledTimerWithTimeInterval方法来每1秒执行一次timeFireMethod函数,timeFireMeth ...
- aop+自定义注解
自定义注解,并且实现,需要两个文件: 自定义注解类: package com.clc.server.annotation; import java.lang.annotation.ElementTyp ...