Le 2 sept. 2011 à 01:48, James Walker a écrit :

> The sample code page 
> <http://developer.apple.com/library/mac/#samplecode/MethodReplacement/Introduction/Intro.html>
>  describes it as
> 
> "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."
> 
> That sounds like something I want to know.  But it says that the document has 
> been "retired".  Anyone know why it has been retired, and whether there is a 
> newer, better way of doing this?
> 

You can use runtime functions. The fact that the sample code is no longer here 
does not mean that the API is deprecated (but its usage is probably discouraged 
yet).

Note: method_exchangeImplementations() must be used with great care. You cannot 
blindly swapping method impl without checking if the method you target is 
defined by the target class.

For example, if you want to replace dealloc in a subclass Foo of NSObject, you 
must ensure that Foo overrides this method, else you're going to exchange the 
implementation on NSObject.
If Foo does not override it, you should create a new Method (using runtime 
functions), and add it to the class.

One of the few place I saw a correct implementation is here:

http://mikeash.com/pyblog/friday-qa-2010-01-29-method-replacement-for-fun-and-profit.html

IIRC, the MethodReplacement sample code was not performing this check, and so 
was broken.

-- Jean-Daniel




_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to