5

I'm new to objective-c and need to extend a standard class of a framework with an instance variable plus accessors. I heard that this is done with a so called "category", which sounds pretty confusing to me. How does this basically work?

Binarian
  • 12,296
  • 8
  • 53
  • 84
Thanks
  • 40,109
  • 71
  • 208
  • 322

2 Answers2

11

A category adds methods to the table of methods inside a class. It's very handy for adding application specific methods to existing framework classes.

If you need to add instance variables to a class, a category won't do the job -- categories only add methods, not data. To add instance variables, you must subclass.

Don McCaughey
  • 9,532
  • 3
  • 30
  • 36
7

A category of a class adds methods to that class. It cannot add instance variables.

If you need to add instance variables you may want to subclass instead.

mouviciel
  • 66,855
  • 13
  • 106
  • 140