(转发)IOS高级开发~Runtime(一)
IOS高级开发~Runtime(一)
IOS高级开发~Runtime(二)
IOS高级开发~Runtime(三)
一些公用类:
@interface CustomClass : NSObject
- (void) fun1;
@end
@implementation CustomClass
- (void) fun1
{
NSLog(@"fun1");
}
@end
@interface TestClass : NSObject
@end
@implementation TestClass
@end
别忘记引入库:#include<objc/runtime.h>
1、对象拷贝:id object_copy(id obj, size_t size)
- (void) copyObj
{
CustomClass *obj = [CustomClassnew];
NSLog(@"%p", &obj);
id objTest = object_copy(obj,sizeof(obj));
NSLog(@"%p", &objTest);
[objTest fun1];
}
打印结果:
2013-07-26 15:35:11.547 HighOC[6859:c07] 0xbfffdf64
2013-07-26 15:35:11.547 HighOC[6859:c07] 0xbfffdf60
2013-07-26 15:35:11.547 HighOC[6859:c07] fun1
说明:
object_copy 函数实现了对象的拷贝。
2、对象释放 id object_dispose(id obj)
- (void) objectDispose
{
CustomClass *obj = [CustomClassnew];
object_dispose(obj);
[obj release];
[obj fun1];
}
打印结果:程序crash
malloc: *** error for object 0x758e6d0: pointer being freed was not allocated
3、更改对象的类/获取对象的类
Class object_setClass(id obj, Class cls) /
Class object_getClass(id obj)
- (void) setClassTest
{
CustomClass *obj = [CustomClassnew];
[obj fun1];
Class aClass =object_setClass(obj, [CustomClassOtherclass]);
//obj
对象的类被更改了 swap the isa to an isa
NSLog(@"aClass:%@",NSStringFromClass(aClass));
NSLog(@"obj class:%@",NSStringFromClass([objclass]));
[obj fun2];
}
- (void) getClassTest
{
CustomClass *obj = [CustomClassnew];
Class aLogClass =object_getClass(obj);
NSLog(@"%@",NSStringFromClass(aLogClass));
}
4、获取对象的类名 constchar
*object_getClassName(id obj)
*obj = [CustomClassnew];
NSString *className = [NSStringstringWithCString:object_getClassName(obj)encoding:NSUTF8StringEncoding];
NSLog(@"className:%@", className);
}
BOOL class_addMethod(Class cls,SEL name,IMP imp,
const
char *types)
一个参数
*
*/
int cfunction(id
self, SEL _cmd,
NSString *str) {
NSLog(@"%@", str);
return10;//随便返回个值
}
- (void) oneParam {
TestClass *instance = [[TestClassalloc]init];
// 方法添加
class_addMethod([TestClassclass],@selector(ocMethod:), (IMP)cfunction,"i@:@");
if ([instance respondsToSelector:@selector(ocMethod:)]) {
NSLog(@"Yes, instance respondsToSelector:@selector(ocMethod:)");
} else
{
NSLog(@"Sorry");
}
int a = (int)[instanceocMethod:@"我是一个OC的method,C函数实现"];
NSLog(@"a:%d", a);
}
/**
* 两个参数
*
*/
int cfunctionA(id
self, SEL _cmd,
NSString *str, NSString *str1) {
NSLog(@"%@-%@", str, str1);
return20;//随便返回个值
}
- (void) twoParam {
TestClass *instance = [[TestClassalloc]init];
class_addMethod([TestClassclass],@selector(ocMethodA::), (IMP)cfunctionA,"i@:@@");
if ([instance respondsToSelector:@selector(ocMethodA::)]) {
NSLog(@"Yes, instance respondsToSelector:@selector(ocMethodA::)");
} else
{
NSLog(@"Sorry");
}
int a = (int)[instanceocMethodA:@"我是一个OC的method,C函数实现"
:@"-----我是第二个参数"];
NSLog(@"a:%d", a);
}
相关文档及说明:
Obj-C的方法(method)就是一个至少需要两个参数(self,_cmd)的C函数
IMP有点类似函数指针,指向具体的Method实现。
向一个类动态添加方法
BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)
参数说明:
cls:被添加方法的类
name:可以理解为方法名
imp:实现这个方法的函数
types:一个定义该函数返回值类型和参数类型的字符串
class_addMethod([TestClass class], @selector(ocMethod:), (IMP)testFunc, "i@:@");
其中types参数为"i@:@“,按顺序分别表示:
i:返回值类型int,若是v则表示void
@:参数id(self)
::SEL(_cmd)
@:id(str)
官方文档:
http://blog.csdn.net/zfpp25_/article/details/9496705
http://blog.csdn.net/zfpp25_/article/details/9497187
http://blog.csdn.net/zfpp25_/article/details/9497721
http://blog.csdn.net/zfpp25_/article/details/9498233
(转发)IOS高级开发~Runtime(一)的更多相关文章
- (转发)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 会提高我们的程序性能和开发速度.要想使用runtime,首先要引入系统的头文件. <span style="font-size:18p ...
- iOS 高级开发 runtime(三)
三 .动态添加方法 我们可以通过runtime动态地添加方法.那么到底啥叫动态添加方法呢?动态添加方法就是当我们程序运行时才知道我们应该调用哪个方法.我们首先需要了解这一点,当我们编写完一段代码后,我 ...
- IOS 高级开发 runtime(二)
二.移魂大法 使用runtime还可以交换两个函数.先贴上代码和执行结果. #import <Foundation/Foundation.h> @interface DZLPerson : ...
- IOS高级开发之多线程(四)NSOperation
1.什么是NSOperation,NSOperationQueue? NSOperation是一个抽象的基类,表示一个独立的计算单元,可以为子类提供有用且线程安全的建立状态,优先级,依赖和取消等操作. ...
- IOS高级开发~Runtime(一)
#import <Foundation/Foundation.h> @interface CustomClass : NSObject -(void)fun1; @end @interfa ...
- IOS高级开发~Runtime(二)
#import <Foundation/Foundation.h> @interface CustomClass : NSObject { NSString *varTest1; NSSt ...
随机推荐
- editplus 3.4注册码,亲测有效
注册码: crsky 7879E-5BF58-7DR23-DAOB2-7DR30
- [HNOI2010] 平面图判定 planar
标签:二分图判定.题解: 首先可以把题目中给你的那个环给画出来,这样就可以发现对于任意一个图来说,如果两条边要相交,就不能让他们相交,那么这两条边就要一条在里面一条在外面,如果把环画成一条链,那么就是 ...
- springboot与shiro和mybatis和mysql
测试项目已上传到GitHub:https://github.com/xiaostudy/springboot_shiro_test1 1.创建springboot项目 <!-- 数据库连接池 - ...
- web前端篇:JavaScript基础篇(易懂小白上手快)-2
目录 一.内容回顾: ECMAScript基础语法 1.基本数据类型和引用数据类型 2.条件判断和循环 3.赋值运算符,逻辑运算符 4.字符串的常用方法 5.数组的常用方法 6.对象 7.函数 8.日 ...
- windows如何搭建redis集群
操作系统:win10 64位 redis版本:3.2.1-x64 ruby版本:2.5.1-1-x64 rubygems版本:2.7.6 今天突然想简单的搭建一个redis的集群,因为系统是Windo ...
- Python爬虫|爬取喜马拉雅音频
"GOOD Python爬虫|爬取喜马拉雅音频 喜马拉雅是知名的专业的音频分享平台,用户规模突破4.8亿,汇集了有声小说,有声读物,儿童睡前故事,相声小品等数亿条音频,成为国内发展最快.规模 ...
- 条件运算符?:接受三个操作数,是C#中唯一的三元运算符(转)
int i = 10; int j = i == 10 ? 1 : 2; //转换成if选择结果如下 if (i == 10) { j = 1; } else { j = 2; } 需要根据还可以嵌套 ...
- gns3 拖出设备显示一个红色的s,无法启动虚拟设备
通过view-docks-调出console窗口,显示错误信息: Error while creating project: Can't connect to server http://172.0. ...
- 编译运行第一个Java程序——通过示例学习Java编程3
作者:CHAITANYA SINGH 来源:https://www.koofun.com//pro/kfpostsdetail?kfpostsid=13 在本教程中,我们将了解如何编写.编译和运行Ja ...
- vue2.0:(一)、vue的安装和项目搭建(以外卖app项目举例)
vue系列踩坑大作战由此就要开始了,准备好了吗,和我一起踩坑,学会vue吧.同时,也欢迎大家把自己遇到的坑发出来,让更多的人学会vue,因为我深知前端学习新框架不容易,尤其是我这种半路出家的女前端.不 ...