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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to