0

what does it mean to create a category like this: @interface myClass () ... @end in the same class's .m file ? this category may contain methods and properties, why not to add these methods and properties directly in the class's .h file ?

thank you in advance.

JAHelia
  • 6,934
  • 17
  • 74
  • 134

3 Answers3

2

It's basically a workaround for Objective-C's lack of private methods. You put classes in there to hide them from users of the class. They can technically still use them (although they'll get a warning) but if they don't know about them, they probably won't.

Randall
  • 14,691
  • 7
  • 40
  • 60
2

Sometimes i would do this when i had a private method.

Because,if the method not define in header,when you call it in implementation,you got a warning, (your method's code is above your invoked).

like this:Instance method '-XXX' not found (return type defaults to 'id')

So,for no warning,i put a method define in a category maybe i will write a @interface myClass(private).

Sven Tan
  • 1,062
  • 7
  • 15
1

If there is no name in the parenthesis it is a class extension. If you define a name then it is a category.

The most common reason to do this is to hide instance variables and methods. To make the appear to be private.

Markus Persson
  • 1,103
  • 9
  • 21