0

我有以下代码来进行方法调配:

static inline void swizzleMethod(const char *clsName, const char *mthName, const char *clsName2, const char *mthName2) {
    Class cls1 = objc_getClass(clsName);
    SEL sel1 = sel_registerName(mthName);

    Class cls2 = objc_getClass(clsName2);
    SEL sel2 = sel_registerName(mthName2);
    Method mth = class_getInstanceMethod(cls2, sel2);
    IMP imp = method_getImplementation(mth);
    NSLog(@"method type encoding %s", method_getTypeEncoding(mth));

    class_replaceMethod(cls1, sel1, imp, method_getTypeEncoding(mth));
}

我的 VGFileHandle 类:

@interface VGFileHandle : NSObject
@end

@implementation VGFileHandle
- (NSData *)readDataToEndOfFile {
    NSLog(@"readDataToEndOfFile");
    return nil;
}
@end

在 main.m 中,我有以下调用:

int main(int argc, char *argv[])
{    
    swizzleMethod("NSFileHandle", "readDataToEndOfFile", "VGFileHandle", "readDataToEndOfFile");

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"myFile"];

    NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
    NSData *data = [handle readDataToEndOfFile];

    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

正确的,当调用 [handle readDataToEndOfFile] 时,它应该从 VGFileHandle 调用 readDataToEndOfFile 并打印出“readDataToEndOfFile”。但是,在我的测试项目中,方法调配似乎根本没有取代方法实现。[handle readDataToEndOfFile] 仍然执行原始实现。

我想知道可能是什么原因。提前致谢。

4

2 回答 2

2

不看 NSFileHandle 的实现,是无法确定的。

很可能是因为 NSFileHandle 是作为类集群实现的,因此,您实际上没有抽象公共类的实例。相反,您有一些私有子类的实例(可能会因看似随机的、面向实现细节的、跨操作系统版本或配置的原因而改变)。

Swizzling 方法是糟糕的代码。私有系统类中的混乱方法是更糟糕的代码,并且几乎肯定会导致调试和支持痛苦。

于 2013-01-25T15:00:23.580 回答
1

[NSFileHandle fileHandleForReadingAtPath:filePath] 将返回 NSConcreteFileHandle 的实例,而不是 NSFileHandle。

您需要改为调用它:

swizzleMethod("NSConcreteFileHandle", "readDataToEndOfFile", "VGFileHandle", "readDataToEndOfFile");
于 2013-03-19T08:43:16.063 回答