Runtime之实例总结
通过前面几篇对Runtime的讲解,本篇汇总一下Runtime实际中常用的一些场景。
1.获取类的基本信息
获取类名:
const char *className = class_getName(class);
获取成员变量:
unsigned int count = ;
Ivar *ivars = class_copyIvarList([GofPerson class], &count); for (int i = ; i < count; i++) {
Ivar ivar = ivars[i];
const char *name = ivar_getName(ivar); NSLog(@"%d.成员变量:%s", i, name);
}
free(ivars);
获取属性:
unsigned int count = ;
objc_property_t *propertys = class_copyPropertyList([GofPerson class], &count); for (int i = ; i < count; i++) {
objc_property_t property = propertys[i];
const char *name = property_getName(property);
const char *attribute = property_getAttributes(property);
NSLog(@"%d.propertyName: %s, attribute: %s",i, name, attribute);
}
free(propertys);
获取类的实例方法:
//获取实例方法列表
unsigned int count = ;
Method *methods = class_copyMethodList(objectClsObj, &count);
for (int i = ; i < count; i++) {
Method methodItem = methods[i];
const char *methodType = method_getTypeEncoding(methodItem);// 获取方法参数类型和返回类型
NSLog(@"instance method item%d:%s %s",i, sel_getName(method_getName(methodItem)), methodType);
}
free(methods);
2.动态创建类和类的基本信息
创建类:
//创建存储空间
Class newClass = objc_allocateClassPair([GofBaseViewController class], "GofClass", ); /**
动态添加方法 @param cls 类类型
@param name 选择器(SEL)
@param imp 函数指针
@param type 方法类型
*/
SuppressUndeclaredSelectorWarning(class_addMethod(newClass, @selector(testMetaClass), (IMP)TestMetaClass, "v@:"));
//注册这个类
objc_registerClassPair(newClass);
添加成员变量:
class_addIvar(newClass, "name", sizeof(id), log2(sizeof(id)), "@");
添加属性:
//添加属性
objc_property_attribute_t attribute1 = {"T", "@\"NSString\""}; //type
objc_property_attribute_t attribute2 = {"C", ""}; //copy
objc_property_attribute_t attribute3 = {"N", ""}; //nonatomic
objc_property_attribute_t attribute4 = {"V", "_email"}; //variable name
objc_property_attribute_t attributesList[] = {attribute1, attribute2, attribute3, attribute4};
if(class_addProperty([GofPerson class], "email", attributesList, )) {
NSLog(@"add property success!");
}
else {
NSLog(@"add property failure!");
}
添加方法:
SuppressUndeclaredSelectorWarning(class_addMethod(objectClsObj, @selector(newMethod), (IMP)testNewMethod, "v@:"));
3.关联对象
@interface GofPerson (GofWork) @property (nonatomic, strong) NSString *workSpace; //!<工作空间 @end static const void *s_WorkSpace = "s_WorkSpace";
@implementation GofPerson (GofWork) - (void)setWorkSpace:(NSString *)workSpace {
objc_setAssociatedObject(self, s_WorkSpace, workSpace, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} - (NSString *)workSpace {
return objc_getAssociatedObject(self, s_WorkSpace);
} @end
4.消息转发/方法交换
详见Runtime之方法。
Runtime之实例总结的更多相关文章
- [深入浅出WP8.1(Runtime)]应用实例——移动截图
10.2应用实例——移动截图 移动截图例子是实现一个把一张图片的某个部分截取出来的功能,并且用户可以选定截取的图片区间.那个该例子会使用ManipulationDelta事件来实现对截取区间的选择.然 ...
- RunTime 应用实例–关于埋点的思考
埋点是现在很多App中都需要用到的,这个问题可能每个人都能处理,但是怎样来减少埋点所带来的侵入性,怎样用更加简洁的方式来处理埋点问题,怎样减少误埋,如果上线了发现少埋了怎么办?下面是本文讨论的重点: ...
- iOS Objc Runtime 教程+实例Demo
样例Demo 欢迎给我star!我会继续分享的. 概述 Objc Runtime使得C具有了面向对象能力,在程序执行时创建,检查.改动类.对象和它们的方法.Runtime是C和汇编编写的,这里http ...
- 深入研究java.lang.Runtime类
一.概述 Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接. 一般不能实例化一个Runtime对象, ...
- 浅析Java.lang.Runtime类
一.概述 Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接. 一般不能实例化一个Runtime对象, ...
- iOS runtime的理解和应用
项目中经常会有一些的功能模块用到runtime,最近也在学习它.对于要不要阅读runtime的源码,我觉得仅仅是处理正常的开发,那真的没有必要,只要把常用的一些函数看下和原理理解下就可以了. 但是如果 ...
- java 23 - 3 单例模式实现Runtime类
Runtime:每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接. 其中一个方法: exec(String command) 在单独的进程中执行指定的字符串 ...
- Runtime.getRuntime().exec()
Runtime.getRuntime()返回当前应用程序的Runtime对象,该对象 的exec()方法指示Java虚拟机创建一个子进程执行指定的可执行程序,并返回与该子进程对应的Process对象实 ...
- 《深入浅出Windows Phone 8.1 应用开发》基于Runtime框架全新升级版
<深入浅出Windows Phone 8.1 应用开发>使用WP8.1 Runtime框架最新的API重写了上一本<深入浅出Windows Phone 8应用开发>大部分的的内 ...
随机推荐
- 代码: jquery 插件开发(自用插件)
http://www.imooc.com/learn/99 阿当大话西游之WEB组件 2016-4-19 jquery插件开发: 2016-3-1 http://www.cnblogs.com/Way ...
- luigi 学习
1.mac 上安装luigi pip install luigi pip install boto3 (luigi依赖 boto3) 2.基本概念 class Streams(luigi.Task): ...
- 两个时间点计算相隔几年,几个月,几天-java
本文采用Calendar 实现 ,当然也可以用java8提供的愉快且方便的时间处理- LocalDate import java.text.ParseException; import java.te ...
- ssh服务器配置
使用如下终端命令可以在 Linux 主机中安装 ssh服务器sudo apt-get install openssh-server lin@lin-machine:~$ sudo apt-get in ...
- 在cxGrid表格中如何获得当前列的字段名
var GridDBTableView:TcxGridDBTableView; ColIndex:Integer; FieldName:string; begin GridDBTableView := ...
- ajax-json,遇到的一个问题,jquery var ,加载顺序。JS对象,json格式转换。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- XSLT 创建CDATA节点
创建文本结点 (1)直接写入文本: text1 (2)通过<xsl:text>创建文本结点: <xsl:text>text2</xsl:text> (3)通过< ...
- 云笔记项目-网页端debug功能学习
在做云笔记项目的过程中,除了服务端在eclipse中debug调试代码外,有时候需要在浏览器端也需要进行debug调试,刘老师举了一个冒泡排序算法的dubug例子,进行了讲解. 首先上浏览器端测试代码 ...
- 在windows下安装Git并用GitHub同步
准备环境: 1,注册github账户 2,下载安装git(下载地址:https://git-scm.com/download/win) 注释: git是什么? git是版本管理工具,当然也是分布式的管 ...
- as3.0 当fla里面有TLF文本的时候,加载声音会出现错误
问题描述 1.现有制作好的mp3加载包,这个包是相对路径 2.如果fla里面没有TLF文本,可以正常运行 解题思路 1.音频的相对路径和加载TLF文本的路径不一样,fla会优先选择TLF文件,这样mp ...