MAObjCRuntime

源码地址:(引入头文件MARTNSObject.h即可,非arc环境下)

http://pan.baidu.com/s/1eQ6776U

https://github.com/mikeash/MAObjCRuntime

MAObjCRuntime is an ObjC wrapper around the Objective-C runtime APIs. If that's confusing, it provides a nice object-oriented interface around (some of) the C functions in /usr/include/objc.

MAObjCRuntime 是对 Objective-C runtime 的 API 的封装。如果你觉得 runtime 的 API 让你迷惑。那这个 MAObjCRuntime 就给你提供了一个对象导向型的接口方便你使用。

Quick Start(快速开始)

The action begins in MARTNSObject.h. Various methods are added to NSObject to allow querying and manipulation. Most of these are class methods, because they operate on classes. There are a couple of instance methods as well. All of these methods start with rt_ to avoid name conflicts. The RTMethod and RTIvar classes are used to represent a single method and a single instance variable, respectively. Their use should be fairly obvious.

你可以在 MARTNSObject.h 文件开始着手。里面提供了很多不同的方法,用来给 NSObject 添加了许多方法,允许你来进行相关查询和操作。绝大部分都为类方法,因为他们都是来操作类的。也有着许多实例方法。所有的方法都以 rt_ 开头,为了避免名字的冲突。

这个 RTMethod 和 RTIvar 分别代表一个方法以及一个实例变量,你看看就知道怎么用了。

Querying(查询)

You can query any class's methods, instance variables, or other attributes using the methods provided. For example:

你可以查询所有类的方法,实例变量,或者属性,例如:

// get all subclasses of a class
NSArray *subclasses = [MyClass rt_subclasses]; // check out the methods on NSString
NSArray *methods = [NSString rt_methods];
for(RTMethod *method in methods)
NSLog(@"%@", method); // does it have any ivars?
NSLog(@"%@", [NSString rt_ivars]); // how big is a constant string instance?
NSLog(@"%ld", (long)[[@"foo" rt_class] rt_instanceSize]);

Modifying(修改)

You can add new methods using +rt_addMethod:. You can modify the implementation of an existing method using the -setImplementation: method on RTMethod. Example:

你可以使用 +rt_addMethod: 添加新的方法。你可以使用 -setImplementation:来修改方法,例如:

// swizzle out -[NSObject description] (don't do this)
static NSString *NewDescription(id self, SEL _cmd)
{
return @"HELLO WORLD!";
} Method *description = [NSObject rt_methodForSelector: @selector(description)];
[description setImplementation: (IMP)NewDescription];

You can create new classes using +rt_createSubclassNamed: or +rt_createUnregisteredSubclassNamed:. Note that if you want to add instance variables to a class then you have to use the Unregistered version, and add them before registering the class.

你可以使用 +rt_createSubclassNamed: 或者 +rt_createUnregisteredSubclassNamed: 创建新的类。注意,如果你想添加实例变量到一个类中,你必须使用未登记的版本,在注册这个类之前添加上他们。

Objects(对象)

Two instance methods are provided as well. -rt_class exists because Apple likes to fiddle with the return value of -class, and -rt_class always gives you the right value. -rt_setClass: does pretty much what it says: sets the class of the object. It won't reallocate the object or anything, so the new class had better have a memory layout that's compatible with the old one, or else hilarity will ensue.

提供了两个实例方法。之所以存在 -rt_class 是因为苹果喜欢瞎折腾 -class 的返回值,所以,-rt_class 能时常给你正确的值。-rt_setClass: 已经够见名知意了。设置这个对象的类。它不会再给这个对象分配更多的空间,所以,新类最好有一个好的内存布局且能兼容设置之前的类,否则会让你癫狂的。

Sending Messages(发送消息)

After getting a list of methods from a class, it's common to want to actually use those on instances of the class.RTMethod provides an easy method for doing this, as well as several convenience wrappers around it.

当你从一个类中得到了一个方法的列表。你肯定就想在这个类的实例对象上试一下效果。RTMethod 提供了一个简单的方法来帮你干这种事情,同时也有一些其它便利的封装来实现这个功能。

The basic method for sending messages is -[RTMethod returnValue:sendToTarget:]. You use it like this:

最基础的用来发送消息的方法是 -[RTMethod returnValue:sendToTarget:],你可以这样子用:

RTMethod *method = ...;
SomeType ret;
[method returnValue: &ret sendToTarget: obj, RTARG(@"hello"), RTARG(42), RTARG(xyz)];

It may seem odd to have the return value at the beginning of the argument list, but this comes closest to the order of the normal ret = [obj method] syntax.

返回值放在第一个位置看起来很奇怪,但是呢,他是最接近普通的 ret = [obj method] 的语法规则了。

All arguments must be wrapped in the RTARG macro. This macro takes care of packaging up each argument so that it can survive passage through the variable argument list and also includes some extra metadata about the argument types so that the code can do some basic sanity checking. No automatic type conversions are performed. If you pass a double to a method that expects an int, this method will abort. That checking is only based on size, however, so if you pass a float where an int is expected, you'll just get a bad value.

所有的参数都必须在 RTARG 宏中封装起来。这个宏用来处理每一个参数,这样才能确保消息在通过不同的变量列表时,有时还包括了一些额外附加的元数据进行基本的检测。没有自动便利的类型转换。如果你传递了个double,而那个方法期望得到int,这个方法就会崩溃。那个仅仅是用来检测基本的类型的size的。然而,如果你传进了一个float参数,但是int是它期望得到的值,你将会得到一个错误的返回值,但不会崩溃。

Note that while it's not 100% guaranteed, this code does a generally good job of detecting if you forgot to use theRTARG macro and warning you loudly and calling abort instead of simply crashing in a mysterious manner. Also note that there is no sanity checking on the return value, so it's your responsibility to ensure that you use the right type and have enough space to hold it.

注意,这个是不能100%确保的,这份代码已经做到了很好的处理了,如果你忘记使用这个宏 RTARG ,那么就会很明显的警告你并直接导致崩溃,而不是简单的不知道啥原因的崩溃。注意,这个并没有检测返回值,如何正确的处理返回值,腾出足够的空间来接收返回值就是你的事情了。

For methods which return an object, the -[RTMethod sendToTarget:] method is provided which directly returns id instead of making you use return-by-reference. This simplifies the calling of such methods:

对于返回一个对象的方法,你可以使用这个 -[RTMethod sendToTarget:] 来直接得到返回的 id ,多方便。

RTMethod *method = ...;
id ret = [method sendToTarget: obj, RTARG(@"hello"), RTARG(42), RTARG(xyz)];

There is also an NSObject category which provides methods that allows you to switch the order around to be more natural. For example:

也有 NSObject 的类目文件,它提供的方法允许你调整顺序让其更加自然,例如:

RTMethod *method = ...;
id ret = [obj rt_sendMethod: method, RTARG(@"hello"), RTARG(42), RTARG(xyz)];

And the same idea for rt_returnValue:sendMethod:.

这个方法 rt_returnValue:sendMethod:也有这功能:

Finally, there are a pair of convenience methods that take a selector, and combine the method lookup with the actual message sending:

最后,有一对便利的方法来获取一个selector,来组合这个方法,看起来更加自然的发送一条消息^_^:

id ret = [obj rt_sendSelector: @selector(...), RTARG(@"hello"), RTARG(42), RTARG(xyz)];
SomeType ret2;
[obj rt_returnValue: &ret2 sendSelector: @selector(...), RTARG(12345)];

[runtime] MAObjCRuntime的更多相关文章

  1. runtime梳理。

    一.runtime简介 RunTime简称运行时.OC就是运行时机制,也就是在运行时候的一些机制,其中最主要的是消息机制. 对于C语言,函数的调用在编译的时候会决定调用哪个函数. 对于OC的函数,属于 ...

  2. myeclipse 无法启动 java.lang.IllegalStateException: Unable to acquire application service. Ensure that the org.eclipse.core.runtime bundle is resolved and started (see config.ini).

    把myeclipse10 按照目录完整拷贝到了另外一台电脑, 另外的目录 原安装目录 D\:\soft\i\myeclipse10 新安装目录 E\:\soft\myeclipse10 双击启动失败, ...

  3. Objective-C runtime初识

    Objective-C Runtime Describes the macOS Objective-C runtime library support functions and data struc ...

  4. Objective-C runtime的常见应用

    用Objective-C等面向对象语言编程时,"对象"(object)就是"基本构造单元"(building block).开发者可以通过对象来存储并传递数据. ...

  5. Runtime应用防止按钮连续点击 (转)

    好久之前就看到过使用Runtime解决按钮的连续点击的问题,一直觉得没啥好记录的.刚好今天旁边同时碰到这个问题,看他们好捉急而且好像很难处理,于是我先自己看看… 前面自己也学习了很多Runtime的东 ...

  6. iOS开发-- 通过runtime kvc 移除导航栏下方的阴影效果线条

    网上查了很多, 都是重新绘制, 感觉有点蠢, 恰巧工作有会闲, 就简单的通过runtime遍历了下属性找寻了下私有类和方法, 这里直接贴方法, 找寻过程也发出来, 能看懂的直接就能看懂, 看不太明白的 ...

  7. VS2015 出现 .NETSystem.Runtime.Remoting.RemotingException: TCP 错误

    错误内容: 界面显示内容为: .NET�������������System.Runtime.Remoting.RemotingException: TCP 淇¢亾鍗忚鍐茬獊: 搴斾负鎶ュご銆� 鍦 ...

  8. DirectX runtime

    DirectX 9.0 runtime etc https://www.microsoft.com/en-us/download/details.aspx?id=7087 DirectX 11 run ...

  9. runtime

    7.runtime实现的机制是什么,怎么用,一般用于干嘛. 你还能记得你所使用的相关的头文件或者某些方法的名称吗? 运行时机制,runtime库里面包含了跟类.成员变量.方法相关的API,比如获取类里 ...

随机推荐

  1. 下载Eclipse

    工欲善其事必先利其器,我们学习Java首先要学会下载开发工具,Eclipse就是一个很好的Java语言开发工具,那么我们首先要知道怎么下载Eclipse.相信很多Java书籍都有下载Eclipse的教 ...

  2. Java访问者模式

    结构对象会遍历它自己所保存的聚集中的所有节点,在本系统中就是节点NodeA和NodeB.首先NodeA会被访问到,这个访问是由以下的操作组成的: (1)NodeA对象的接受方法accept()被调用, ...

  3. Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) E - Aquarium decoration 贪心 + 平衡树

    E - Aquarium decoration 枚举两个人都喜欢的个数,就能得到单个喜欢的个数,然后用平衡树维护前k大的和. #include<bits/stdc++.h> #define ...

  4. CentOS 配置自启动Redis

    第一步: 在/etc/init.d/目录下建立一个名字为 redis 的启动脚本 cd /etc/init.d touch redis 然后在这个脚本中添加如下脚本  <注意修改自己的PIDFI ...

  5. MySQL性能优化之char、varchar、text的区别

    参考来源:https://blog.csdn.net/brycegao321/article/details/78038272 在存储字符串时, 可以使用char.varchar或者text类型, 那 ...

  6. JAVAEE——BOS物流项目14:Linux部署(安装jdk、tomcat、mySQL)和扩展资料

    1 学习计划 1.Linux部署 n 安装jdk n 安装tomcat n 安装MySQL n 将项目发布到tomcat 2.扩展资料 2 Linux部署 2.1 安装jdk 第一步:获取Linux系 ...

  7. liniux Crontab 的重启和设置

    重启crontab,service cron restart 05 01 * * * /usr/local/php/bin/php FILE 10,30,50 * * * * /usr/local/p ...

  8. BZOJ 1449: [JSOI2009]球队收益 最小费用最大流 网络流

    https://www.lydsy.com/JudgeOnline/problem.php?id=1449 给每条路加上一个权值,每条路的费用是这条路的流量*权值,求最大流的最小费用. 每次spfa记 ...

  9. 浅谈期望的线性性(可加性)【CodeForces280c】【bzoj3036】【bzoj3143】

    [pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=63399955 向大(hei)佬(e)势力学(di ...

  10. hdu 4858 水题

    题意:我们建造了一个大项目!这个项目有n个节点,用很多边连接起来,并且这个项目是连通的!两个节点间可能有多条边,不过一条边的两端必然是不同的节点.每个节点都有一个能量值.现在我们要编写一个项目管理软件 ...