class_getClassMethod 通常返回 nil (似乎仅适用于类级别方法)
我目前正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系,我的错误,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