Search code examples
objective-ccategoriesprivate-methodsclass-extensionsobjc-category

objective C explanation about categories?


in my noob mind and proceedings, when i create a category for a class, I create it in a new file

like NSArray+Shuffle.h and .m

but I have noticed that when I create for example a ViewController,

i have in the implementation:

@interface PingusViewController ()

@end

@implementation PingusViewController
...
@end

So my question is:

what is the

@interface PingusViewController ()

@end

part?, is this for categories? or what use?, and if it is for categories, why use it here and not in some new files?

thanks!


Solution

  • It's called a Class Extension.

    Consider it the conventional location for your class' private declarations.

    It's normally declared in the implementation file rather than a header file because the declarations are considered private, and are intended to be visible to the class' @implementation only.

    Categories OTOH, are interfaces which the author typically uses to extend the class' public interface. The declaration is similar -- after all, the Class Extension is just an unnamed category, but the applications are distinct (by convention).