Detecting conflicting Objective-C category methods

Objective-C categories are an extremely powerful way to add functionality to an existing class. Take for example, the category below that adds some stack-like methods to NSMutableArray:

@interface NSMutableArray (StackAdditions)
- (id)pop;
- (void)push:(id)object;
@end

@implementation NSMutableArray (StackAdditions)
- (id)pop
{
    id lastObject = [[[self lastObject] retain] autorelease];
    [self removeLastObject];
    return lastObject;
}

- (void)push:(id)object
{
    [self addObject:object];
}
@end

Now let’s say that I was writing a plugin for any app in Mac OS X that had a plugin architecture, such as IB, Aperture, Address Book, etc. Now, let’s also say that these apps had the same category methods on NSMutableArray, but they were slightly different, maybe they didn’t retain the object because of a special need they had. Well, when my plugin gets loaded into the app, the Objective-C runtime will auto-magically replace their category with mine because they have methods with the same name.

Obviously, this presents a problem. Now whenever their code calls -[NSMutableArray pop] or -[NSMutableArray push:], then the runtime will use my implementation instead. And if their category relied on some special happenings in their implementation, then the entire app could quickly fall into an unstable and fatal state.

So what’s to be done? If you are writing an application that has a plugin architecture or you are writing a plugin for an app, you should run the application during testing with the environment variable OBJC_PRINT_REPLACED_METHODS set to YES. What this does is it tells the Objective-C runtime to print out any methods it replaces when loading in a plugin. If you see that your implementation is being overwritten or you are overriding someone else’s, then you know that you will need to change the name of your method to something else, such as -[NSMutableArray myPop] instead of the original method -[NSMutableArray pop].