Re: Forcing allocation of a subclass
Re: Forcing allocation of a subclass
- Subject: Re: Forcing allocation of a subclass
- From: Antonio Nunes <email@hidden>
- Date: Sat, 24 Jan 2009 22:07:44 +0100
On 24 Jan 2009, at 19:20, Bill Bumgarner wrote:
If so, the method_exchangeImplementations() pattern will work. If
not, you'll need to add the method to the class via class_addMethod().
Let's see if I understand this correctly: you mean I need to add
alloc, or as suggested, allocWithZone: zone to the PDFClerkDocument
class, and after that, I suppose, exchange the method implementations?
Make use of Bill's and Jean-Daniel's suggestions I now have:
Method originalMethod = class_getClassMethod([PDFDocument class],
@selector(allocWithZone:));
Method superMethod =
class_getClassMethod(class_getSuperclass([PDFDocument class]),
@selector(allocWithZone:));
if (superMethod == originalMethod) {
// PDF Document does not override allocWithZone:
IMP replacement = class_getMethodImplementation([PDFDocument class],
@selector(replacementAllocWithZone:));
BOOL success = class_addMethod([PDFDocument class],
@selector(allocWithZone:),
replacement,
"@@:@");
}
I verified that the method is added correctly. However, the
replacement method appears not to be called, so I must be doing
something wrong.
From my understanding, the second argument to class_addMethod is the
name the added method will have, so that should be
replacementAllocWithZone, the third argument is the IMP of the
replacement, and the last argument is the array of characters
describing the of the method arguments, resp an object as the return
type, self and cmd and the NSZone object. Is this correct?
-António
-----------------------------------------------------------
Some things have to be believed to be seen.
--Ralph Hodgson
-----------------------------------------------------------
_______________________________________________
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