ios-simulator - class_getClassMethod 通常返回 nil(似乎只适用于类级别的方法)

标签 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

最佳答案

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

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

抱歉打扰了

P.B

关于ios-simulator - class_getClassMethod 通常返回 nil(似乎只适用于类级别的方法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3422693/

相关文章:

ios - 无法写入使用 CFStreamCreatePairWithSocket 成功创建的 CFWriteStream

ios - 从 Github 加载可在其他几台 Mac 上运行的库时,Xcode 5 模拟器不显示所有 View

ios - 在 iOS 5.0 模拟器中使用 UIManagedDocument 时出现 NSFileCoordinator 错误

c - getchar() 和 printf() 内部到底发生了什么?解释一下整个过程

java - 了解泛型

iphone - 为什么此音频单元RemoteIO初始化可在iPhone上但不能在模拟器中使用?

powershell - 检查 Powershell 中是否存在通讯组

vba - 使用 EntryID、StoreID 和/或 PR_ENTRYID 打开 Outlook 邮件项目

java - 使用 MS Exchange 服务器从 java 发送邮件,导致连接超时

spring 接口(interface)作为@ModelAttribute 参数