Re: IB instantiating objects
Re: IB instantiating objects
- Subject: Re: IB instantiating objects
- From: Gmail <email@hidden>
- Date: Tue, 24 Mar 2009 12:47:04 +0700
I'm sorry! The light bulb just went off as soon as I sent that last
message. I'm NOT supposed to init a NEW instance, but use that
instance and simply modify it, which works perfectly. Please ignore
that last message, this issue is solved and thank you very much for
helping. Pascal users on Mac thank you also.
On Mar 24, 2009, at 11:22 AM, Ashley Clark wrote:
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
Regards,
Josef
_______________________________________________
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