Search code examples
objective-cpragma

Xcode - implementing a method, also be implemented by its primary class


I'm using Xcode 4.5. In my latest Xcode project, I have this warning pop up when i build/compile my program:

Category is implementing a method which will also be implemented by its primary class

This is my code that is causing the error:

 @implementation NSApplication (SDLApplication)
 - (void)terminate:(id)sender {
    SDL_Event event;
    event.type = SDL_QUIT;
    SDL_PushEvent(&event); }
 @end

I have made it so the compiler ignores (or skips over) the warning generate using pragma code.

So my code now looks like this:

 @implementation NSApplication (SDLApplication)

 #pragma clang diagnostic push
 #pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
 - (void)terminate:(id)sender{
   SDL_Event event;
   event.type = SDL_QUIT;
   SDL_PushEvent(&event);}
 #pragma clang diagnostic pop
 @end

Obviously it does the job and once built/compiled, no warning is generated.

My question is, is this a safe/ok way to suppress or ignore the warning in this way?

I read on some threads that using subclasses were advantageous, but there were many people fore and against using them in this way... I'd appreciate your thoughts :)


Solution

  • "I read on some threads that using subclasses were advantageous..."

    Using subclasses may be advantageous but that's not what you are doing here.

    What you have done is implemented an (SDLApplication) Objective-C Category on NSApplication. In that category, you are overriding NSApplication's terminate: method. In general, you should use categories only to add additional methods to an existing class, not to override existing methods, as that can have unpredictable results.

    What you really should do is subclass NSApplication so that you can override terminate: properly:

    @interface SDLApplication : NSApplication {
    
    }
    
    @end
    
    
    @implementation
    
    - (void)terminate:(id)sender {
       SDL_Event event;
       event.type = SDL_QUIT;
       SDL_PushEvent(&event);
       [super terminate:sender]; // this line is the key to assuring proper behavior
    }
    
    @end
    

    The last step in your overridden terminate: method should be a call to super, so that the terminate method works as it should. (Using the keyword super in a category is not allowed; only the word self, which is why there's a need for subclassing).

    Make sure you also change the value for the NSPrincipalClass key in your Info.plist file to be SDLApplication instead of NSApplication.