Retired Document
Important: This sample code may not represent best practices for current development. The project may use deprecated symbols and illustrate technologies and techniques that are no longer recommended.
MethodReplacement
Last Revision: | Version 1.0, 2006-08-01 Demonstrates an Objective C 2 compatible way to replace a method in an existing Objective C class. |
Build Requirements: | Leopard Preview |
Runtime Requirements: | Leopard Preview |
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.
Copyright © 2006 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2006-08-01