替换Objective-C Foundation函数的实现 [英] Replacing Objective-C Foundation function implementations

查看:76
本文介绍了替换Objective-C Foundation函数的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有办法替换Objective-C Foundation函数的实现,例如NSClassFromString函数?我当然知道

Is there a way to replace the implementation of an Objective-C Foundation function, such as the NSClassFromString function for example? I am of course aware of class_replaceMethod, but it seems to work only for class methods and not general functions.

推荐答案

这些函数只是C函数和/或宏(Objective C基于C构建),您不能替换C函数.

Those functions are just C functions and/or macros (Objective C is built on C), and you can't replace a C function.

但是,您可以使用define屏蔽名称:

However, you can use a define to mask the name:

Class MyNSClassFromString( NSString* blah ) {
    return whatever;
}

#define NSClassFromString MyNSClassFromString

从那时起,调用NSClassFromString(@"hi")将调用自定义函数.请注意,这有一些局限性:它不会更改define以上的任何代码(或正在使用的库中的任何代码),通常会使智能感知不满意,并且如果定义了NSClassFromString,它将无法正常工作.首先是宏(在这种情况下,可以在定义之前添加#undef NSClassFromString.)

Now from that point on, calling NSClassFromString(@"hi") will call the custom function. Note this has several limitations: It will not change any code which is above the define (or any code within libraries you're using), it will often make intellisense unhappy, and it won't work if NSClassFromString is defined as a macro in the first place (you can add #undef NSClassFromString before the define in that case).

通常,我不建议这样做,但是如果您真的想要这样做,则有可能.

Generally, I wouldn't recommend this, but if you really want to do it, the possibility is there.

这篇关于替换Objective-C Foundation函数的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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