通过前面几篇对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之实例总结的更多相关文章

  1. [深入浅出WP8.1(Runtime)]应用实例——移动截图

    10.2应用实例——移动截图 移动截图例子是实现一个把一张图片的某个部分截取出来的功能,并且用户可以选定截取的图片区间.那个该例子会使用ManipulationDelta事件来实现对截取区间的选择.然 ...

  2. RunTime 应用实例–关于埋点的思考

    埋点是现在很多App中都需要用到的,这个问题可能每个人都能处理,但是怎样来减少埋点所带来的侵入性,怎样用更加简洁的方式来处理埋点问题,怎样减少误埋,如果上线了发现少埋了怎么办?下面是本文讨论的重点: ...

  3. iOS Objc Runtime 教程+实例Demo

    样例Demo 欢迎给我star!我会继续分享的. 概述 Objc Runtime使得C具有了面向对象能力,在程序执行时创建,检查.改动类.对象和它们的方法.Runtime是C和汇编编写的,这里http ...

  4. 深入研究java.lang.Runtime类

    一.概述      Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接.      一般不能实例化一个Runtime对象, ...

  5. 浅析Java.lang.Runtime类

    一.概述      Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接.      一般不能实例化一个Runtime对象, ...

  6. iOS runtime的理解和应用

    项目中经常会有一些的功能模块用到runtime,最近也在学习它.对于要不要阅读runtime的源码,我觉得仅仅是处理正常的开发,那真的没有必要,只要把常用的一些函数看下和原理理解下就可以了. 但是如果 ...

  7. java 23 - 3 单例模式实现Runtime类

    Runtime:每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接. 其中一个方法: exec(String command) 在单独的进程中执行指定的字符串 ...

  8. Runtime.getRuntime().exec()

    Runtime.getRuntime()返回当前应用程序的Runtime对象,该对象 的exec()方法指示Java虚拟机创建一个子进程执行指定的可执行程序,并返回与该子进程对应的Process对象实 ...

  9. 《深入浅出Windows Phone 8.1 应用开发》基于Runtime框架全新升级版

    <深入浅出Windows Phone 8.1 应用开发>使用WP8.1 Runtime框架最新的API重写了上一本<深入浅出Windows Phone 8应用开发>大部分的的内 ...

随机推荐

  1. 新部署tomcat,An error occurred at line: [1] index_jsp.java

    环境: centos6.5 32位 oracle jdk 1.8 tomcat 7 问题: yum install tomcat后,返回如下错误: [root@centos]~# curl -v ht ...

  2. sybase-sql语法-replace用法

    1.去空格 update hyl_temp02 --去空格 set acc_nbr=replace(acc_nbr,' ',''); commit; 2.去回车 update hyl_temp02 - ...

  3. Halcon示例:bottlet.hdev 光学字符识别(创建OCR)

    * * Training of the OCR* The font is used in "bottle.hdev"* * * Step 0: PreparationsFontNa ...

  4. VideoView 监听视频格式不支持时的错误。

    视频播放格式不支持的处理https://www.cnblogs.com/ygj0930/p/7737209.html 不处理的情况下,默认会有弹框提示:不支持该视频格式. mVideoView.set ...

  5. AS3中的单件(Singleton)模式

    单件(singleton)模式在c#中是最容易实现的模式,其主要用意就在于限制使用者用new来创建多个实例.但在as3中,构造函数必须是public的(语法本身要求的),而且也不能在构造函数中抛出异常 ...

  6. [Solution] 973. K Closest Points to Origin

    Difficulty: Easy Problem We have a list of points on the plane. Find the K closest points to the ori ...

  7. 373. Find K Pairs with Smallest Sums 找出求和和最小的k组数

    [抄题]: You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. D ...

  8. 关于java中分割字符串

    例子:String path = "123.456.789"; 如果要使用“.”将path分割成String[], path.split("//."); or ...

  9. sbb指令

    sbb是带借位减法指令,它利用了CF位上记录的借位值. 指令格式:sbb 操作对象1,操作对象2 功能:操作对象1=操作对象1-操作对象2-CF 比如指令sbb ax,bx实现的功能是: (ax)=( ...

  10. spring整合kafka(配置文件方式 消费者)

    Kafka官方文档有   https://docs.spring.io/spring-kafka/reference/htmlsingle/ 这里是配置文件实现的方式 先引入依赖 <depend ...