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

时间:2010-08-06 09:57:10

标签: ios-simulator exchange-server implementation objective-c-runtime

我目前正在尝试使用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

1 个答案:

答案 0 :(得分:9)

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

例如,使用... class_getInstanceMethod ...:)

对不起麻烦

P.B

相关问题