• 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: IB instantiating objects
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: IB instantiating objects


  • Subject: Re: IB instantiating objects
  • From: Ashley Clark <email@hidden>
  • Date: Mon, 23 Mar 2009 23:22:18 -0500

On Mar 23, 2009, at 10:31 PM, Gmail wrote:

On Mar 24, 2009, at 10:01 AM, Ashley Clark wrote:

On Mar 23, 2009, at 9:30 PM, Gmail wrote:

Thanks! I think that document explains everything I need to know to take control over IB.

This is more Objective-C related by maybe you have a quick tip. My first tests suggest that my method for overriding is not correct because overriding "init" is getting invoked from all sorts of other classes (like NSFileManager to name a few) when the NIB is loading. I use class_getInstanceMethod (with the instance of the custom class I registered with the objective-c runtime) to get the method then replace the implementation with my function pointer. This method worked before for overriding drawRect: in NSView so I'm not sure what is different now. Any ideas?

class_getInstanceMethod searches the entire class hierarchy. So, if the class you're working with didn't override that implementation you'll be replacing the superclass' implementation.


If you call class_addMethod on self with the result of class_getMethodImplemenation you'll be adding it to the subclass if it's not already overridden. After that calling method_exchangeImplementations will only affect that class.

Hmmm, I overrode the superclasses implementation, i.e. NSObject, which sounds about right considering the results.


So, I need to be using class_addMethod for "init" because my new class has no implementation for that method. I think the previous attempt to override NSView worked because drawRect: DID have an implementation for that selector already added to the runtime, which is not the case with my new class. Thank you Ashley I think you are correct.

Is there anyway way to determine if a class has an implementation for a method so I can decide to add a new method or override an existing? As it stands I don't know in code when I should add or exchange implementations.

You could call class_copyMethodList and iterate through that to find only methods defined directly on a class, but there's no harm in just calling class_addMethod. It will add an override of a superclass' implementation but will not replace an existing implementation if one was already defined on the class.


So, there's no need to decide between add or exchange. Do both!


eg. (typed in Mail, YMMV)

Class klass = ...
SEL selector = ...
Method originalMethod = class_getInstanceMethod(klass, selector);

class_addMethod(klass, selector, class_getMethodImplementation(klass, selector), method_getTypeEncoding(originalMethod));

class_replaceMethod(...);


You'll get the right behavior every time, assuming "right" means you only want to replace the method on the class and not its' superclasses.



Ashley _______________________________________________

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


  • Follow-Ups:
    • Re: IB instantiating objects
      • From: Gmail <email@hidden>
    • Re: IB instantiating objects
      • From: Gmail <email@hidden>
References: 
 >IB instantiating objects (From: Gmail <email@hidden>)
 >Re: IB instantiating objects (From: Bill Bumgarner <email@hidden>)
 >Re: IB instantiating objects (From: Gmail <email@hidden>)
 >Re: IB instantiating objects (From: Jonathan Hess <email@hidden>)
 >Re: IB instantiating objects (From: Gmail <email@hidden>)
 >Re: IB instantiating objects (From: Ashley Clark <email@hidden>)
 >Re: IB instantiating objects (From: Gmail <email@hidden>)

  • Prev by Date: Re: -hexValue for NSString?
  • Next by Date: Re: Stop NSScrollView from catching scroll events.
  • Previous by thread: Re: IB instantiating objects
  • Next by thread: Re: IB instantiating objects
  • Index(es):
    • Date
    • Thread