title: Can't swizzle class methods
body: <p>I'm working on making a plugin for Mail.app.  I'd like the plugin to add a button to Mail's toolbar.  To do this, I decided the best approach would be to call the function to add this button in the MessageViewer's initialize method (MessageViewer is the class for Mail.app's FirstResponder).  The code I'm modifying seems to work nicely:</p>
  
  <pre><code>+ (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 &amp;= [[self class] copyMethod:@selector(_specialValidateMenuItem:) 
                                   fromClass:[self class] 
                                     toClass:MessageViewer];
  swizzleSuccess &amp;= [[self class] copyMethod:@selector(unsubscribeSelectedMessages:) 
                                   fromClass:[self class] 
                                     toClass:MessageViewer];
  </code></pre>
  
  <p>However, when I try to do the same, it doesn't work:</p>
  
  <pre><code>//  //Copy the method to the original MessageViewer
  swizzleSuccess &amp;= [[self class] copyMethod:@selector(_specialInitMessageViewer:)
                                   fromClass:[self class]
                                     toClass:MessageViewer];  
  </code></pre>
  
  <p>Here are the swizzling methods:</p>
  
  <pre><code>+ (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;
  }
  </code></pre>
  
  <p>It seems to be failing in the call to class_getInstanceMethod, because I get an error in the log.  This happens for both the method inside of my own class, as well as for the MessageViewer's initialize method.  </p>
  
  <p>Are there some gotchas I'm not taking into account here?  </p>