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)

官方文档:

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html



IOS高级开发~Runtime(一)

http://blog.csdn.net/zfpp25_/article/details/9496705

IOS高级开发~Runtime(二)

http://blog.csdn.net/zfpp25_/article/details/9497187

IOS高级开发~Runtime(三)

http://blog.csdn.net/zfpp25_/article/details/9497721

IOS高级开发~Runtime(四)

http://blog.csdn.net/zfpp25_/article/details/9498233

(转发)IOS高级开发~Runtime(一)的更多相关文章

  1. (转发)IOS高级开发~Runtime(四)

    用C代替OC: #import <objc/runtime.h> #import <objc/message.h> #import <stdio.h> extern ...

  2. (转发)IOS高级开发~Runtime(三)

    11.系统类的方法实现部分替换 - (void) methodExchange { Method m1 = class_getInstanceMethod([NSStringclass],@selec ...

  3. (转发)IOS高级开发~Runtime(二)

    一些公用类: @interface ClassCustomClass :NSObject{ NSString *varTest1; NSString *varTest2; NSString *varT ...

  4. IOS高级开发 runtime(一)

    一. 简介 IOS 开发中灵活使用runtime 会提高我们的程序性能和开发速度.要想使用runtime,首先要引入系统的头文件. <span style="font-size:18p ...

  5. iOS 高级开发 runtime(三)

    三 .动态添加方法 我们可以通过runtime动态地添加方法.那么到底啥叫动态添加方法呢?动态添加方法就是当我们程序运行时才知道我们应该调用哪个方法.我们首先需要了解这一点,当我们编写完一段代码后,我 ...

  6. IOS 高级开发 runtime(二)

    二.移魂大法 使用runtime还可以交换两个函数.先贴上代码和执行结果. #import <Foundation/Foundation.h> @interface DZLPerson : ...

  7. IOS高级开发之多线程(四)NSOperation

    1.什么是NSOperation,NSOperationQueue? NSOperation是一个抽象的基类,表示一个独立的计算单元,可以为子类提供有用且线程安全的建立状态,优先级,依赖和取消等操作. ...

  8. IOS高级开发~Runtime(一)

    #import <Foundation/Foundation.h> @interface CustomClass : NSObject -(void)fun1; @end @interfa ...

  9. IOS高级开发~Runtime(二)

    #import <Foundation/Foundation.h> @interface CustomClass : NSObject { NSString *varTest1; NSSt ...

随机推荐

  1. 百度搜索:有关Baiduspider的10个问题

    猫宁!!! 参考链接: http://help.baidu.com/question?prod_id=99&class=476&id=2996 https://ziyuan.baidu ...

  2. A - Beautiful numbers

    #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> ...

  3. [coci2015-2016 coii] torrent【树形dp 二分】

    传送门:http://www.hsin.hr/coci/archive/2015_2016/ 进去之后点最下面那个. 这道题没有想出来,可惜了,其实不难的. 题目是两个“源”的,我们先考虑单源的问题. ...

  4. JavaScript引擎基本原理:Shapes和Inline Caches

    原文链接: JavaScript engine fundamentals:Shapes and line Cahes 这篇文章描述了一些在js引擎中通用的关键点, 并不只是V8, 这个引擎的作者(Be ...

  5. [译]Understanding ECMAScript 6 内容目录

    说明 浏览器与Node.js兼容 这本书是写给谁的 概述 帮助与支持 基本知识 更好的Unicode支持 其他字符串变化 其他正则表达式变化 Object.is() 块绑定 解构赋值 数字 总结 函数 ...

  6. TDH-search常用命令

    一.指令部分:1.search管理界面地址: http://172.20.230.110:9200/_plugin/head/ 2.集群状态查看命令: curl -XGET 'localhost:92 ...

  7. QQ免费企业邮箱申请配置

    对于小企业来说,免费的企业邮箱是不错的选择,省去服务器费用和人员维护费用.在这里说一下,qq的免费企业邮箱.如果想搭建自己的企业邮局,请参考:centos extmail postfix nginx ...

  8. Nginx upstream负载均衡配置

    1.在http节点下添加 upstream test {     server 127.0.0.1:16010;     server 127.0.0.1:16011; } 2.把server 节点下 ...

  9. 基于.NET网页开发的工作,需要掌握的知识点

    学习计划对于程序员来说尤为重要,我最近根据自己的职业规划和招聘网站上对于基于.NET网页开发工作所需要的技能做出了一个总结,这个总结的内容也将是自己最近一年的知识补充和学习的方向,各位园友也可以把它作 ...

  10. puppeteer 中国区的使用

    puppeteer 中国区的使用 [issues]https://github.com/GoogleChrome/puppeteer/issues/1426 两种方案 使用 cnpm .npmrc 中 ...