无法调配类方法

Aar*_*ron 4 macos objective-c 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];
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试做同样的事情时,它不起作用:

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

以下是混合方法:

+ (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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

NSR*_*der 9

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


Lil*_*ard 5

您应该只使用JRSwizzle,而不是尝试手动实现调配.需要注意的一点是,JRSwizzle的实现+jr_swizzleClassMethod:withClassMethod:error:只是一个存根,但你可以简单地调用+jr_swizzleMethod:withMethod:error类的元类(例如,只是传递klass->isa而不是klass(klass你正在调整的类在哪里)).