Objective-C为对象的每种方法添加功能 [英] Objective-C add functionality to every method of an object

查看:67
本文介绍了Objective-C为对象的每种方法添加功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为objective-c中的对象构建一个小型插件系统。

i want to build a small plugin-system for objects in objective-c.

现在,我被困在想要动态(运行时)的地方在对象中的每个可用函数中添加一行代码。

Now i am stuck at the point where i want to dynamically (at runtime) add a line of code to every function available in an object.

我曾经在运行时库中玩过,但仍未解决。

I have played around with the runtime library but came to no solution, yet.

到目前为止,我一直在尝试以下操作:

What i have tried so far is this:

        id (^impyBlock)(id, id, ... ) = ^(id self_, id arguments, ...)
        {
            // My custom code for every function here
            id (*func)(__strong id,SEL,...) = (id (*)(__strong id, SEL, ...))imp;
            return func(obj, s, arguments);

        };

        id (*impyFunct)(id, SEL,...) = imp_implementationWithBlock(impyBlock);
        method_setImplementation(mList[i], impyFunct);

我的问题是,当有多个论点时,我没有机会将它们传递给 func ()。 AFAIK在C中是不可能的。

My problem is, when there is more than one argument i got no chance to pass them to "func()". AFAIK this is not possible in C.

我想到的另一种解决方案是用方法混淆来做一些魔术。

Another solution i have thought about is doing some magic with method swizzling.

分步进行:


  1. 创建一个转换方法,该方法仅调用我的自定义代码,然后调用原始方法(通过命名模式) )

  2. 使用转换方法之一更改每个函数的IMP。

  3. 使用旧实现创建新方法并进行更改命名为 ___name之类的架构

在此解决方案中,我停留在第3点。我没有设法动态创建完全新的方法。

In this solution i am stuck at point 3. I haven't managed to dynamically create a complete new method.

有人可以帮助我解决上述问题吗?或者有另一种解决方案来捕获所有方法功能。

Does anybody can help me with my problems above or has another solution for a "catch all method functionality".

最好是诸如forwardInvocation之类的东西,它也可以捕获已定义的函数。

The best would be something like forwardInvocation which also catches already defined functions.

感谢您的帮助!

推荐答案

Lemme将其分为两部分ce我只是无法获得您的两个问题之间的联系。

Lemme break this into two parts since I just can't get the connection between your two questions.

我。用旧实现创建一个新方法,然后将其名称更改为 ___name之类的模式。

I. Create a new method with the "old" implementation and change to name to a schema like "___name"

这很容易,尽管我不知道那会是什么。能够解决您的问题。您仍然无法将可变参数函数的参数传递给这种方法(是的,这是用C语言无法完成的。)。

That's fairly easy, although I don't understand how that would be able to solve your problem. You still can't pass down variadic function arguments to such a method (and you're right, that can't be done in C).

IMP swapImpForSelector(Class cls, SEL sel, IMP newImp)
{
    Method m = class_getInstanceMethod(cls, sel);
    IMP oldImp = method_setImplementation(m, newImp);

    NSString *newSel = [NSString stringWithFormat:@"__prefixed_%@", NSStringFromSelector(sel)];
    const char *type = method_getTypeEncoding(m);
    class_addMethod(cls, NSSelectorFromString(newSel), oldImp, type);

    return oldImp;
}

II。如果要在函数之间传递可变参数,则可能需要依靠繁重的汇编程序。幸运的是,一些聪明的人已经为您做到了。

II. If you want to pass variadic arguments between functions, you may need to fall back to heavy assembly hackage. Fortunately, some smart people have already done that for you.

都可以使用 NSInvocation类,或者如果不够用,请 libffi 甚至更低级。

Either use the NSInvocation class, or if it isn't sufficient, then libffi is even lower-level.

这篇关于Objective-C为对象的每种方法添加功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆