¿Cómo evitar la advertencia de compilación al subclasificar una clase con categoría?

Say we have parent class


@interface ParentViewController




@interface ParentViewController()

- (NSArray *)selectedItems;


@implementation ParentViewController



And then we subclass it


@interface ChildViewController : ParentViewController




@implementation ChildViewController

- (void)doSomething
    // XCode Warning Flag on this line
    NSUInteger count = [self selectedItems];


XCode will set Warning flag at the commented line and says that "Instance method '-selectedItems' not found (return type defaults to 'id').

Yes I know that in ObjC there is no such thing as private methods, but using an empty category kind of gives the ability to do so. But somehow it does not get inherited by subclasses.

I usually fix it by moving the method from ParentViewController.m to ParentViewController.h. This feels weird, I loose the ability to make the method private just because I need to subclass it.

Ahora mi pregunta es:

  1. Why does the parent subclass cannot find those methods that is declared in its category at the .m file?

  2. Is there a way to remove the Warning Flag but without losing the ability to keep the method private.

Hopefully someone with more experience will be able to help explain this annoying issue.

preguntado el 10 de marzo de 12 a las 01:03

2 Respuestas

First, note that your "empty category" isn't a Category at all, it's a Class Extension. Class Extensions very similar to categories but they're new in Objective C 2.0, and they differ slightly in their use. Primarily, the compiler will warn you if you don't implement a method in a Class Extension, but it won't with a Category. Anyways, on to the issue at hand...

Privacy in Objective-C is all about visibility. If the compiler can't see see the declaration of a method that's being used, you'll get a warning. Note that if you were to implement your subclass in the same file as your Class Extension, the compiler won't warn you because it can see the declaration.

Therefore, If you want to use "private" methods in subclasses, you just need some way of showing the compiler that those methods exist. My favorite pattern is to declare the private methods in a Category in a separate file (like MyClass_private.h). You then only import that interface into the implementation files of the base class and any derived classes that need to see it.

respondido 10 mar '12, 02:03

Yep this works. Combining parent and subclass in the same .m file is not an option because they are big. It is cumbersome to place those private methods in a separate file. I will accept this answer, i don't think there will be any other way. - Samuel Chandra

@SamuelChandra, yeah, the same-file point was just trivia. You should probably never do that. - mate salvaje

I have a solution, but generally I would advise against it. if you compile the file with -w (suppress all warnings), the warning goes away. I do not know if there is a specific warning flag for this message, if there was, you could use #pragma GCC diagnostic ignored "-Winstance-method-not-found", but I can't find it, sorry.

respondido 10 mar '12, 02:03

+1: Correct, but I would also advise against it. Those warning are useful 99% of the time :) - mate salvaje

-w suppress all warnings, is not an option that I can take unfortunately. Thanks - Samuel Chandra

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.