views:

53

answers:

1

Since class_poseAs(..) is deprecated in Objective-C 2.0, I need to find another way to change the class of an object at runtime. I've found I can change an object's class using object_setClass(..). My problem now is finding all the current instances of a given class in order to update them.

A solution would be to maintain a global dictionary of the instances I want to potentially update, but I'd like to know whether there's already a way to obtain such a collection from the reflective api.

I checked Apple's Runtime reference and I cannot find anything useful. Updating just the method of a class with method_exchangeImplementations(..) is not a solution for me since I want to be able to reuse the old implementation by using super.

+2  A: 

Generally if you want to exchange the implementation of various methods in a particular class for your own, you'd use the method_exchangeImplementations() function in the Objective-C 2.0 runtime. (You'll need to #import objc/runtime.h). This is far simpler than trying to swap out the actual class, as poseAsClass: is deprecated and unavailable in the 64-bit runtime.

Rob Keniger
As I mention in the third paragraph, I really need to change the whole class and not only the methods. I want to take advantage of the OO-dispatch mechanism to reuse the original code. (The new class for the object would be a subclass or the original one).
BaDGeR