Re: Forcing allocation of a subclass
Re: Forcing allocation of a subclass
- Subject: Re: Forcing allocation of a subclass
- From: Mike Abdullah <email@hidden>
- Date: Sat, 24 Jan 2009 12:33:26 +0000
I would recommend against the category approach. As your search of the
archives no doubt explained, there's no guarantee that PDFDocument may
at some point internally use its own category that would override yours.
I don't know exactly how you'd do what you want (+poseAsClass: is
deprecated) with the runtime functions, but may I advise that whatever
your eventually solution you replace +allocWithZone: rather than +alloc
Mike.
On 24 Jan 2009, at 09:19, Antonio Nunes wrote:
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
_______________________________________________
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