通过前面几篇对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. mybatis的Selective接口和普通接口的区别

    举例说明: updateByPrimaryKeySelective会对字段进行判断再更新(如果为Null就忽略更新),如果你只想更新某一字段,可以用这个方法. updateByPrimaryKey对你 ...

  2. leetcode32

    class Solution { public: int longestValidParentheses(string s) { ; stack<int> st; ; i < n; ...

  3. leetcode105

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  4. Rabbitmq(5) 路由模式

    设置路由键 发送者 package com.aynu.bootamqp.service; import com.aynu.bootamqp.commons.utils.Amqp; import com ...

  5. spring boot+kafka整合

    springboot版本是2.0.4 首先,在maven中引入spring-kafka的jar包 <dependency> <groupId>org.springframewo ...

  6. Tomcat-servlet基础

    1.1 概念 运行在服务器上的小程序   定义了浏览器访问到(tomact)的规则 1.2 步骤 1.3 执行原理 1  当服务器 接收到客户端浏览器的请求后  会解析url地址  获得url路径   ...

  7. python 网络编程 缓冲和粘包

    tcp:属于长连接,与一个客户端进行连接了以后,其他的客户端要等待,要连接另外一个,必须优雅的断开前面这个客户端的连接. 允许地址重用:在bind IP地址和端口之前加上,# server.setso ...

  8. python小练习1:设计这样一个函数,在桌面的文件夹上创建10个文本,以数字给它们命名。

    python小练习1:设计这样一个函数,在桌面的文件夹上创建10个文本,以数字给它们命名. 使用for循环即可实现: for name in range(1,11): desktop_path='C: ...

  9. javascript函数闭包(closure)

    一,首先感受下javascript函数的闭包 二,闭包 1,定义:闭包就是能够读取其他函数内部变量的函数,由于在javascript语言中,只有在函数内部的子函数才能够读取局部变量,因此可以把闭包简单 ...

  10. 学生管理系统(Java Swing JDBC MySQL)

    该系统使用 Java Swing.JDBC.MySQL 开发 开发环境 Eclipse.WindowBuilder JDK版本:1.8 代码在百度网盘中(176***5088) 目录结构如下 Data ...