2010-12-08 69 views
4

我正在为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的初始化方法。

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

+0

您的代码格式不正确,看起来有点乱。您需要每条代码行至少有4个空格才能使其不被破坏。 – 2010-12-08 02:10:00

回答

5

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

+0

谢谢Kevin,JRSwizzle包看起来棒极了!现在,我仍然负责将我的类方法复制到要调试的类中,还是有方法可以在jr_swizzleClassMethod调用中指明此方法? (我很抱歉,如果它很明显;我是ObjC的新手) – Aaron 2010-12-08 18:17:53

9

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

相关问题