无法调配类方法

时间:2010-12-08 02:06:14

标签: objective-c macos nsbundle

我正在为Mail.app制作一个插件。我想要插件为Mail的工具栏添加一个按钮。为此,我决定最好的方法是调用函数在MessageViewer的initialize方法中添加此按钮(MessageViewer是Mail.app的FirstResponder的类)。我正在修改的代码似乎工作得很好:

+ (void) initialize
{  
[super initialize];

//  class_setSuperclass([self class], NSClassFromString(@"MVMailBundle"));
//  [ArchiveMailBundle registerBundle];


// Add a couple methods to the MessageViewer class.
Class MessageViewer = NSClassFromString(@"MessageViewer");

// swizzleSuccess should be NO if any of the following three calls fail
BOOL swizzleSuccess = YES;
swizzleSuccess &= [[self class] copyMethod:@selector(_specialValidateMenuItem:) 
                                 fromClass:[self class] 
                                   toClass:MessageViewer];
swizzleSuccess &= [[self class] copyMethod:@selector(unsubscribeSelectedMessages:) 
                                 fromClass:[self class] 
                                   toClass:MessageViewer];

但是,当我尝试这样做时,它不起作用:

//  //Copy the method to the original MessageViewer
swizzleSuccess &= [[self class] copyMethod:@selector(_specialInitMessageViewer:)
                                 fromClass:[self class]
                                   toClass:MessageViewer];  

以下是混合方法:

+ (BOOL)swizzleMethod:(SEL)origSel withMethod:(SEL)altSel inClass:(Class)cls
{
// For class (cls), swizzle the original selector with the new selector.
    //debug lines to try to figure out why swizzling is failing.
//  if (!cls  ||  !origSel) {
    //          NSLog(@"Something was null.  Uh oh.");
    //}
Method origMethod = class_getInstanceMethod(cls, origSel);

if (!origMethod) {
    NSLog(@"Swizzler -- original method %@ not found for class %@", NSStringFromSelector(origSel), 
          [cls className]);
    return NO;
}
    //if (!altSel) NSLog(@"altSel null.  :(");
Method altMethod = class_getInstanceMethod(cls, altSel);
if (!altMethod) {
    NSLog(@"Swizzler -- alternate method %@ not found for class %@", NSStringFromSelector(altSel), 
          [cls className]);
    return NO;
}

method_exchangeImplementations(origMethod, altMethod);

return YES;

}


+ (BOOL) copyMethod:(SEL)sel fromClass:(Class)fromCls toClass:(Class)toCls
{
    // copy a method from one class to another.
    Method method = class_getInstanceMethod(fromCls, sel);
    if (!method)
    {
        NSLog(@"copyMethod-- method %@ could not be found in class %@", NSStringFromSelector(sel),
              [fromCls className]);
        return NO;
    }
    class_addMethod(toCls, sel, 
                    class_getMethodImplementation(fromCls, sel), 
                    method_getTypeEncoding(method));
    return YES;
}

在调用class_getInstanceMethod时似乎失败了,因为我在日志中收到错误。对于我自己的类内部的方法以及MessageViewer的initialize方法都会发生这种情况。

我在这里没有考虑到一些问题吗?

2 个答案:

答案 0 :(得分:9)

如果你想调用类方法,那么为什么使用class_getInstanceMethod()函数而不是class_getClassMethod()

答案 1 :(得分:5)

您应该只使用JRSwizzle,而不是尝试手动实施调配。需要注意的是,JRSwizzle对+jr_swizzleClassMethod:withClassMethod:error:的实现只是一个存根,但您可以简单地在类的元类上调用+jr_swizzleMethod:withMethod:error(例如,只需传递klass->isa而不是klass(其中klass是您正在调配的课程)。