Re: swizzling a class to a subclass inorder to add ivars (& methods) to a class

  • Oh, I thought poseAsClass: did allow the use of additional instance
    variables.  Sorry!

    Mike.

    On 30 Oct 2006, at 17:36, Scott Morrison wrote:

    > Hi Mike
    >
    > Thanks for the response.  I am familiar with poseas and very
    > familiar with swizzling but the limitations of both is that they
    > don't allow for new ivars to the posed class or swizzle (really a
    > category).
    >
    > Swizzling the alloc to create an instance of the subclass would
    > allow this -- unless it is very bad practice
    >
    > Scott
    >
    > On 30-Oct-06, at 3:54 AM, Mike Abdullah wrote:
    >
    >> Hi Scott, perhaps I'm being stupid here, but how about using
    >> poseAsClass: ?
    >>
    >> Mike.
    >>
    >> On 30 Oct 2006, at 04:08, 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      (<Cocoa-dev...>)
    >>> Help/Unsubscribe/Update your Subscription:
    >>> http://lists.apple.com/mailman/options/cocoa-dev/mike.abdullah%
    >>> 40gmail.com
    >>>
    >>> This email sent to <mike.abdullah...>
    >>
    >
    > ________________________________
    > Scott Morrison <smorr...>
    > Mail Act-On and Mail Tags plug-ins for OS X Mail.app.  <http://
    > www.indev.ca/>
    >
    >
    >
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today