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: Sun, 25 Jan 2009 08:25:34 +0100
On 24 Jan 2009, at 23:19, Jean-Daniel Dupas wrote:
Calling the original implementation in a method you have exchanged
is done using
[self replacedMethodName]
and in a method added at runtime, you have to call
[super originalMethodName]
[...]
if you don't want to bother with the type string, just query it
using runtime functions:
And you want to add a class method, not an instance method. You have
to add this method to the 'PDFDocument class' meta-class.
Method originalMethod = class_getClassMethod([PDFDocument class],
@selector(allocWithZone:));
Method superMethod =
class_getClassMethod(class_getSuperclass([PDFDocument class]),
@selector(allocWithZone:));
Method replacedMethod = class_getClassMethod([PDFDocument class],
@selector(replacementAllocWithZone:));
if (superMethod == originalMethod) {
class_addMethod(object_getClass([PDFDocument class]),
@selector(allocWithZone:),
method_getImplementation(replacedMethod),
method_getDescription(replacedMethod)->types);
}
Thanks for supplying the last bits to the puzzle Jean-Daniel. Initial
testing seems to indicate the replacement is working. This is what I
have now:
Early during app startup is the code adding the replacement method to
the class, as quoted above.
In a category on PDFDocument is the replacement method itself, which
now reads:
@implementation PDFDocument (PDFDocument_Alloc)
+ (id)replacementAllocWithZone:(NSZone *)zone
{
if (self == [PDFDocument class]) {
return [ANPDFDocument replacementAllocWithZone:zone];
} else {
return [super allocWithZone:zone];
}
}
@end
Many thanks to both Bill and Jean-Daniel for helping me through this,
while pointing out the potential pit-falls. Here's to hoping an update
to Leopard will soon allow me to disable this unfortunate workaround
for it and future updates.
-António
-----------------------------------------------------------
Don't believe everything you think
-----------------------------------------------------------
_______________________________________________
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