Re: IB instantiating objects
Re: IB instantiating objects
- Subject: Re: IB instantiating objects
- From: Gmail <email@hidden>
- Date: Tue, 24 Mar 2009 12:39:48 +0700
Great, thanks. I almost have this working but with one little problem.
1) I add the "init" method to my class
2) When init is invoked from the NIB I call objc_msgSend with the
selector "init" which returns the new instance of the class (retrieved
from objc_getClass) I previously registered with the runtime and added
instance variables.
3) I call class_getInstanceVariable to get the instance variable I
need to assign the reference to the wrapper class.
Here class_getInstanceVariable returns nil which it never did before.
I'm using the 1.0 runtime for now btw, but I get the class of the
instance by using the "isa" field of the structure and give that class
as the parameter to class_getInstanceVariable. This worked with other
objects so I think I may have created the instance wrong. Is simply
sending "init" to the class not enough? I tried calling "alloc" also
but it entered into an infinite loop.
Also, do I need to being the "self" parameter in this process that is
the first parameter in the method? I find it strange I have another
instance of this object I'm trying to replace, what should I do with
it? Is it leaking memory?
Thanks for helping with this I appreciate it.
On Mar 24, 2009, at 11:22 AM, Ashley Clark wrote:
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.
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