• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Replacement for MethodReplacement?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Replacement for MethodReplacement?


  • Subject: Re: Replacement for MethodReplacement?
  • From: Jean-Daniel Dupas <email@hidden>
  • Date: Fri, 02 Sep 2011 10:08:01 +0200

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 (email@hidden)

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:

This email sent to email@hidden

References: 
 >Replacement for MethodReplacement? (From: James Walker <email@hidden>)

  • Prev by Date: NSScrollView animation (animate scroll point)
  • Next by Date: Re: Overwhelming Options
  • Previous by thread: Replacement for MethodReplacement?
  • Next by thread: Re: Large over 100K pixel high ruler scroll view
  • Index(es):
    • Date
    • Thread