Widgets Is it better to just not use Categories?
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 25, 2015, 10:04:39 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Chapter 11
| | |-+ Is it better to just not use Categories?
Pages: [1] Go Down
Author Topic: Is it better to just not use Categories? (Read 1832 times)
Full Member
Posts: 175


on: 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?

Posts: 16

Reply #1 on: 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!

Posts: 4


Reply #2 on: 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 Smiley

Posts: 16

Reply #3 on: 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).

Last Edit: February 17, 2012, 08:53:46 AM by ljaygould Logged
Hero Member
Posts: 3114

Reply #4 on: 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.


Pages: [1] Go Up
Jump to:

Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 All rights reserved.