Version: 1.0

Posted: 2006-08-01

Build Requirements: Leopard Preview

Runtime Requirements: Leopard Preview

View Source Code:

Download Sample (“MethodReplacement.zip”, 38.6K)
Download Sample (“MethodReplacement.dmg”, 95.2K)



Description

Objective C 2.0 compatible class_poseAs() replacement. This demonstrates how to replace a method in an existing Objective C class, and how to call the old version without having to store IMP function pointers in a global. The trick is to define a category on the class whose method you want to replace. In the category, declare and implement a method with the same signature as the replaced method. In the +load method of the category, look up the original and replacement methods using class_getInstanceMethod(), then call method_exchangeImplementations(). Subsequent calls to the original method will call the replacement method, and an apparently recursive call to the replacement method will call the original method.

Document Revision History

Date Notes
2006-08-01Demonstrates an Objective C 2 compatible way to replace a method in an existing Objective C class.

Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.