11

I've recently discovered categories and was wondering when it might be appropriate to use them in a user defined class/new class. For example, I can see the benefits of adding a category to an existing class like NSString, but when creating a new class what would be the advantage of adding a category to this rather than just implementing a normal method?

Hope this makes sense. Many thanks

Jules

Black Frog
  • 11,595
  • 1
  • 35
  • 66
Jules
  • 660
  • 8
  • 29
  • possible duplicate of [Difference between inheritance and Categories in Objective-c](http://stackoverflow.com/questions/522341/difference-between-inheritance-and-categories-in-objective-c) – Black Frog Apr 13 '11 at 11:37

4 Answers4

8

The answer isn't really any different for your own classes than it is for framework classes. If you have multiple projects, you'll likely end up sharing some classes between them. However, you may want to extend some of your classes so that they work more easily with a specific project, but not want to include those extra methods in your other projects, where they might not make sense. You can use a category to extend your class without needing to subclass.

Caleb
  • 124,013
  • 19
  • 183
  • 272
6

If I understand your question correctly, creating a "new class" is always "subclassing" because you're subclassing NSObject at the very least.

You could use categories on a new class to separate out sections of responsibility of a complex class. For example, all the basic functionality (instance variables, accessors, description, etc.) can go in one file (the "main" class file) while all methods to support a protocol (such as NSTableViewDataSource) can go in another.

Some take this approach to keep things "neat". I'm a firm believer in "if it's my own custom class, all its code should be in one file" so I do not personally do this. I demarcate different logical aspects of the class' code with "#pragma mark Some Section Name" to help navigation and readability. Your mileage may vary.

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
  • Ok this makes sense I suppose. Although I think I would tend to take the same approach as you. So from what I have understood the main advantage of Categories as far as Im concerned would be when used with an existing class ie NSString. – Jules Apr 13 '11 at 11:34
  • +1 for "You could use categories on a new class to separate out sections of responsibility of a complex class." and the protocol scenario. Good thought. – Philip007 Dec 04 '12 at 14:26
  • 2
    I'd like to see a more in-depth discussion on "categories on a new class to separate out sections of responsibility of a complex class" vs. "#pragma mark Some Section Name". – Stian Høiland May 21 '14 at 17:21
1

Adding a Category on NSString is useful when you want to call a method on every single NSString instance you will encounter. This is a real improvement over inheritance for this kind of object because they are used by the core framework and you don't have to convert a NSString object to your subclass when you want to call your custom method.

On the other hand, you can just put methods in, no instance variables.

scalbatty
  • 778
  • 4
  • 12
  • 3
    personally, I am a strong enemy of using categories for lazy extension of standard classes. makes the sources harder to understand for third parties as they are not expecting non-standard methods within a standard class. – Till Apr 13 '11 at 11:33
  • Yes I understand that it is useful when used with a class such as NSString. My question was more to do with when they would be useful with a class I have written ie a Person Class. – Jules Apr 13 '11 at 11:36
  • +1 What if we don't want to use some where else. Can we remove a category added earlier. – SNR Apr 13 '11 at 11:37
  • Till- Nice point, although if the code will never be seen it should be fine to use? – Jules Apr 13 '11 at 11:37
  • raj2razz- I think if the category would only be used once then it makes more sense to just have a method within the class which needs it? – Jules Apr 13 '11 at 11:38
  • You mean "when is it useful with a custom class" ? It can be useful if you have a class with many different responsibilities and you want to separate them in several code files I guess, but it should stay an exceptional case. Other than that, I can't see a valid point for Categories on custom classes. – scalbatty Apr 13 '11 at 11:41
  • Thanks Luzal... Thats what I thought – Jules Apr 13 '11 at 11:47
  • @Jules, NEVER assume that noone will be looking at your code. Objective C is an object oriented language. Object oriented coding implies reusing. Your future projects may reuse code you write now and those may involve multiple coders/parties/reviewers. – Till Apr 13 '11 at 12:26
0

In the book Refactoring by Martin Fowler, he has a section titled "Introduce Foreign Method" (A server class you are using needs an additional method, but you can't modify the class.) That's what categories are good for.

That said, there are times when using a category, instead of changing the class, is appropriate. A good example on using a category, even though you could change the server class, is how Apple handled the UIViewController MediaPlayer Additions. They could have put these two methods in UIViewController itself but since the only people who would ever use them are people who are using the Media Player framework, it made more sense to keep the methods there.

Daniel T.
  • 32,821
  • 6
  • 50
  • 72