class_getClassMethod 通常返回 nil (似乎仅适用于类级别方法)

发布于 2024-09-12 19:05:48 字数 2023 浏览 5 评论 0原文

我目前正在尝试使用 http://sudzc.com/ 生成的代码 此代码并不完全适合我的 Web 服务,因此我尝试将类别添加到某些生成的类中,并使用“objc/runtime.h”中的 method_exchangeImplementations 与原始类交换其实现。 (我可以直接修改生成的代码,但我想避免它)。

这是我在 MyAppAppDelegate - applicationDidFinishLaunching 方法中执行的代码

Class theClass = [CBMayaIPhoneUser class];
Method originalMethod = class_getClassMethod(theClass, @selector(initWithNode:));
Method categoryMethod = class_getClassMethod(theClass, @selector(initWithAllStringNode:));
method_exchangeImplementations(originalMethod, categoryMethod);

theClass = [Soap class];
originalMethod = class_getClassMethod(theClass, @selector(getNodeValue:withName:));
categoryMethod = class_getClassMethod(theClass, @selector(getHrefNodeValue:withName:));
method_exchangeImplementations(originalMethod, categoryMethod);

theClass = [SoapRequest class];
originalMethod = class_getClassMethod(theClass, @selector(send));
categoryMethod = class_getClassMethod(theClass, @selector(sendIgnoringCertificate));
method_exchangeImplementations(originalMethod, categoryMethod);
originalMethod = class_getClassMethod(theClass, @selector(connectionDidFinishLoading:));
categoryMethod = class_getClassMethod(theClass, @selector(connectionDidFinishLoadingAndSentBody:));
method_exchangeImplementations(originalMethod, categoryMethod);

正如我的问题中所述,几乎所有这些 class_getClassMethod 都返回 nil...我使用了调试器,所以我知道“theClass”设置正确。找到的唯一方法是 Soap 类的方法,它们都是类级别 (+) 方法。但从网上的各种例子中,我得出的结论是它也应该适用于其他人...

以下是我对 MyAppAppDelegate.m 的包含:

#import "MyAppAppDelegate.h"
#import "RootViewController.h"
#import "MyGlobalVariables.h"
#import "MyWebServiceExample.h"
#import "Soap+Href.h"
#import "SoapRequest+Certificate.h"
#import "CBMayaIPhoneUser+AllString.h"
#import "objc/runtime.h"

我也测试了我的类别并且它们有效,我可以从“originalClass”调用类别方法目的。

我想我做错了什么,但我看不到什么......或者也许是 class_getClassMethod 确实应该只适用于类级别方法吗?

哦,最后一件事,我是在模拟器上开发的,而不是设备上:)

欢迎任何想法!

谢谢

P.B

Im currently trying to use some generated code from http://sudzc.com/
This code is not perfectly adapted to my web services, so I tried to add Categories to some of the generated classes and to exchange their implementations with the original ones using method_exchangeImplementations from "objc/runtime.h". (I could modify the generated code directly but I want to avoid it).

Here is the code I execute in MyAppAppDelegate - applicationDidFinishLaunching method

Class theClass = [CBMayaIPhoneUser class];
Method originalMethod = class_getClassMethod(theClass, @selector(initWithNode:));
Method categoryMethod = class_getClassMethod(theClass, @selector(initWithAllStringNode:));
method_exchangeImplementations(originalMethod, categoryMethod);

theClass = [Soap class];
originalMethod = class_getClassMethod(theClass, @selector(getNodeValue:withName:));
categoryMethod = class_getClassMethod(theClass, @selector(getHrefNodeValue:withName:));
method_exchangeImplementations(originalMethod, categoryMethod);

theClass = [SoapRequest class];
originalMethod = class_getClassMethod(theClass, @selector(send));
categoryMethod = class_getClassMethod(theClass, @selector(sendIgnoringCertificate));
method_exchangeImplementations(originalMethod, categoryMethod);
originalMethod = class_getClassMethod(theClass, @selector(connectionDidFinishLoading:));
categoryMethod = class_getClassMethod(theClass, @selector(connectionDidFinishLoadingAndSentBody:));
method_exchangeImplementations(originalMethod, categoryMethod);

As stated in my question, nearly all of those class_getClassMethod are returning nil... I used the debugger so I know 'theClass' is rightly set. The only method being found are those of the Soap class, which are both class level (+) methods. But from various examples on the net I concluded that it should work for the others as well...

Here are my includes for MyAppAppDelegate.m :

#import "MyAppAppDelegate.h"
#import "RootViewController.h"
#import "MyGlobalVariables.h"
#import "MyWebServiceExample.h"
#import "Soap+Href.h"
#import "SoapRequest+Certificate.h"
#import "CBMayaIPhoneUser+AllString.h"
#import "objc/runtime.h"

I tested my categories too and they work, I can call the category methods from a 'originalClass' object.

I suppose I'm doing something wrong, but I can't see what... Or maybe class_getClassMethod
is indeed supposed to work only for class level methods ?

Ho and last thing, Im developing on the simulator, not the device :)

Any thought is welcome !

Thanks

P.B

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁许谁一生繁华 2024-09-19 19:05:48

没关系,我的错误,class_getClassMethod 确实应该只适用于类级别的方法(顾名思义)

对于实例方法,使用... class_getInstanceMethod... :)

带来麻烦

抱歉给P.B

Never mind, my mistake, class_getClassMethod is indeed supposed to work only for class level methods (as the name implies)

For instance methods, use... class_getInstanceMethod... :)

Sorry for the trouble

P.B

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文