Re: swizzling a class to a subclass inorder to add ivars (& methods) to a class
Re: swizzling a class to a subclass inorder to add ivars (& methods) to a class
- Subject: Re: swizzling a class to a subclass inorder to add ivars (& methods) to a class
- From: Greg Parker <email@hidden>
- Date: Thu, 2 Nov 2006 11:57:15 -0800
Dustin Robert Kick wrote:
Can you access:
struct objc_ivar_list *ivars;
from a class, taking it and an objc_ivar_list that you create, and
set the "ivars" of the class to be a new objc_ivar_list created by
appending the original, and the new ivars you wish to add?
struct objc_ivar_list * newlist = (objc_ivar_list *) malloc (sizeof
(original_list) + sizeof(new_list));
then set all the ivars?
Short answer: Don't do that. It won't work. You can't add ivars to a
class at runtime.
Long answer: Don't do that. It won't work. You can add ivars to a
class at runtime, but only if you're very careful.
To add ivars to a class, do this:
1. Update cls->instance_size.
2. Change cls->ivars if you want key-value coding to work.
This may fail if:
* there are any subclasses of this class that themselves have ivars
* there are any already-allocated instances of this class
* someone else tries to pose as this class
* someone tries to use key-value observing on instances of this class
* the class expects the indexed ivar storage to follow its last real
ivar
* ... and probably others I haven't thought of.
Finally, none of this will work in 64-bit programs, because cls-
>instance_size and cls->ivars won't be available directly.
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden