• 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: swizzling a class to a subclass inorder to add ivars (& methods) to a class
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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: Dustin Robert Kick <email@hidden>
  • Date: Wed, 1 Nov 2006 21:39:18 -0600

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?


Something like that?


Dustin Kick


On Oct 29, 2006, at 10:08 PM, Scott Morrison wrote:


The mantra for categories and posed class is that you cannot add ivars to a category or a posed class -- leaving you to fiddle with mutable dictionarys and tablemaps to implement extra variables. (ugh!)


However, you can add ivars to a subclass -- so I got to thinking -- can you redirect an allocation for a class to a subclass so that for all intents and purposes you can add ivars.

In playing around, I found that by swizzling the classMethod: + [NSObject alloc], you can redirect the allocation of a class to a subclass as follows:
//-------------------------------------------------------------------- --------------------------------------------------------------------


@interface SubClass: AnyClass
{
	NSString * extraIvar;
}

@end

static IMP oldAlloc = NULL;
@implementation NSObject (myCategory)
+(id) MYalloc{
id result;

if(self == [AnyClass class]){
self = objc_getClass("SubClass"); //restate the class as the subclass
result = NSAllocateObject(self,0,nil); // allocate the subclass
}
else
result = oldAlloc(self,_cmd);
return result;
}


+ (void) load {
oldAlloc = replaceClassMethod(@selector(alloc),self,@selector (MYalloc),self); // standard class method swizzle the alloc method of NSObject
}
//-------------------------------------------------------------------- --------------------------------------------------------------------



One catch would be that any factory methods and any instance init methods may have to be reproduced in the subclass if you want to init your subclass ivars
eg.


(id) init___:(id) anObject{
	if ((self = [super init___:anObject])){
		extraIvar = [[NSString alloc] initWithString:@"test iVar"];
	}
	return self;
}


Another catch would be that any test directly for an instance's class type would fail (but tests for isKindOfClass: would be ok)



In anycase -- technically this is possible -- I have it working on some test code


However -- IS IT SAFE ??  -- if no -- why not ---
if  "sorta" -- what are the gotchas and pitfalls?




_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40mac.com


This email sent to email@hidden

_______________________________________________ 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
  • Follow-Ups:
    • Re: swizzling a class to a subclass inorder to add ivars (& methods) to a class
      • From: Ofri Wolfus <email@hidden>
    • Re: swizzling a class to a subclass inorder to add ivars (& methods) to a class
      • From: Bill Bumgarner <email@hidden>
    • Re: swizzling a class to a subclass inorder to add ivars (& methods) to a class
      • From: Scott Morrison <email@hidden>
  • Prev by Date: Re: Posting Global hot keys from a Cocoa app
  • Next by Date: Re: swizzling a class to a subclass inorder to add ivars (& methods) to a class
  • Previous by thread: Re: Carbon-dev Digest, Vol 3, Issue 732
  • Next by thread: Re: swizzling a class to a subclass inorder to add ivars (& methods) to a class
  • Index(es):
    • Date
    • Thread