views:

82

answers:

1

Method swizzling works great for instance methods. Now, I need to swizzle a class method. Any idea how to do it?

Tried this but it doesn't work:

void SwizzleClassMethod(Class c, SEL orig, SEL new) {

Method origMethod = class_getClassMethod(c, orig);
Method newMethod = class_getClassMethod(c, new);

if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
    class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
else
    method_exchangeImplementations(origMethod, newMethod);
}
+2  A: 

Turns out, I wasn't far away. This implementation works for me:

void SwizzleClassMethod(Class c, SEL orig, SEL new) {

    Method origMethod = class_getClassMethod(c, orig);
    Method newMethod = class_getClassMethod(c, new);

    c = object_getClass((id)c);

    if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
        class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
    else
        method_exchangeImplementations(origMethod, newMethod);
}
Ortwin Gentz
Mmm ... Let's say we instead did `Class cc=object_getClass((id)c)`. It's understandable that you need to add the method to `cc`, but why did you take the class method from `cc`? Aren't you supposed to get the class method from `c` ? I'm confused ...
Yuji
Yuhi, you're right, I've changed the code to do the method resolution on the original class, not the meta class. The old solution worked as well so I didn't really bother.
Ortwin Gentz