Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan

Programming in Objective-C, 4th edition => Chapter 11 => : dharr19 February 07, 2012, 10:03:54 AM

: Is it better to just not use Categories?
: dharr19 February 07, 2012, 10:03:54 AM

After reading about Categories it seems that it might almost be better to not use them at all.  What are your thoughts?


: Re: Is it better to just not use Categories?
: ljaygould February 15, 2012, 10:41:26 AM
It looks like a feature that can create a lot of clarity, particularly in a project with many classes, etc.

The problem is that either the implementation, OR the methodology described in the book, breaks the synthesized instance variables when the files are broken out into class+category h and m files. I wish Steve would drop a pearl of wisdom on us here...did WE do something wrong, or is there a missing link in the Xcode environment, or is something else afoot here? See my post (near yours) on another section of this chapter's forum.

For the moment, I returned the Categories interface and implementation to the bottom of the respective Class files (in this case Fraction.h and Fraction.m). I look forward to breaking them back out into the (now-empty) Fraction+MathOps files when we have a better explanation of how!


: Re: Is it better to just not use Categories?
: BombermanX February 17, 2012, 04:16:36 AM
 'Bcharna'  had it right in another post. If you are using separate files for your categories you need to use the 'self' reference...

It makes sense when you think that the function of Categories is to add methods to classes you probably have no implementation (and iVar) knowledge of. You, therefore, need to use accessor methods to access iVars, not access them directly - which is what we were doing in the Function exercise. All examples and posts I researched today on categories used self to access iVars through methods.

Hope that makes sense :)

: Re: Is it better to just not use Categories?
: ljaygould February 17, 2012, 07:50:02 AM
I have only one question left: What needs to be in the main.m file for #import? I note that the Category MathOps is not referenced in Fraction.h and thus to get my program to work I would need to #import Fraction+MathOps.h - and I'm not sure that's practical if there were multiple categories in a class, multiple classes in the project etc. Do we just end up with a very large number of #import statements?

Thanks so much for your post, though. It obviously is what needed to be done for the project to work (i.e. the self. references before the synthesized variables).


: Re: Is it better to just not use Categories?
: skochan February 17, 2012, 04:24:30 PM
Don't forget that categories allow you to add methods to classes that you don't have access to; e.g., the NSArray or NSString classes.  Also, if you had a bunch of category extensions for the same class in different files, you could create a single header file that imported the different category header files.  For example, FractionExtensions.h.



Sorry, the copyright must be in the template.
Please notify this forum's administrator that this site is missing the copyright message for SMF so they can rectify the situation. Display of copyright is a legal requirement. For more information on this please visit the Simple Machines website.