Forcing allocation of a subclass
Forcing allocation of a subclass
- Subject: Forcing allocation of a subclass
- From: Antonio Nunes <email@hidden>
- Date: Sat, 24 Jan 2009 10:19:58 +0100
Hi,
I need to be able to force the requested allocation of a cocoa class
to always return an instance of my subclass. I have looked into ways
of doing that.
I have a solution that seems to work, by using a category on the class
to replace the regular invocation, but from the archives I understand
that is a Bad Idea that can lead to all manner of unpredictable and
hard to trace errors. The current implementation looks like this:
@implementation PDFDocument (PDFDocument_Alloc)
+ (id)alloc
{
if ([[self class] isEqual:[PDFDocument class]]) {
return [ANPDFDocument alloc];
} else {
return [super alloc];
}
}
@end
As I wrote, this appears to work fine. But if indeed this technique is
better avoided, what would be the best alternative? I have looked at
class_replaceMethod, but am having difficulties implementing it. What
would be the best way to implement a safe alternative to the method
listed above?
-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