views:

734

answers:

3

Apple has deprecated NSObject's poseAsClass: method for OS X v10.5 and above. Is there another way to make class posing work?

+4  A: 

Lap Cat gives an alternative.

mcandre
+1 and thank you for the excellent link
e.James
+4  A: 

I don't think there is a class-level equivalent, but you can exchange the implementation of two methods, which was often the purpose of using poseAsClass: (of course, you can exchange more than one method if you need to override multiple methods in a class). You want method_exchangeImplementations in the Objective-C 2.0 runtime (#import objc/runtime.h). A word of warning: after calling method_exchangeImplementations, calling the 'new' method actually calls the original method definition.

Barry Wark
Thank you! I managed to get this to work, using a combination of mcandre's answer and the documentation that you linked to. A quick note: I had to `#include "objc/runtime.h"` in order to use the methods you describe.
e.James
+2  A: 

What are you trying to do? There are often ways around posing. I will concede, though, that it is sometimes the only way :)

Jonathan Dann
I'm trying to keep track of all binding (and unbinding) messages in a Cocoa application. I want to `NSLog()` some debug information whenever a binding is made between one object and another. I ended up using `method_exchangeImplementations` on the binding methods for `NSObject`, and it did the trick.
e.James
Sounds like you're Doing It Right :) I'd be wary of doing it in shipping code.
Jonathan Dann