Initializer chain omission


#1

Certainly not a big deal but this caused me some momentary confusion when I got to chapter 3.
I think the initializer chain discussion is supposed to include the addition of one more initialization method for the Possession class (probably a hold over from the iPhone programming book). “- (id)initWithPossessionName:(NSString *)name”. In chapter 3 this method is shown as part of Possession.h (page 70 of the printed text). This caused me to go back and try to find where I neglected to include this method but its never mentioned. The method doesn’t appear to ever get called by any subsequent exercises so it’s apparently just some cruft.
As an aside, does anyone know how to detect methods that are not called within a project in XCode?


#2

Most likely a typo or maybe a hold over as you suggested from another edition(?) (I’m on 2nd Ed.). The code portion shown on Page 70 is created on Page 49 and it doesn’t show the code repeated as it does on Page 70.

As for the question of checking for uncalled methods, I have no help for you as I am a beginner to Xcode and Objective-C programming. Best of luck! If I discover anything in the future I will try to remember to come back and post my findings.


#3

That question makes more sense with languages that are less dynamic than is Objective-C. In those languages, you write methods (or functions), then either you call them explicitly (which the compiler can tell at compile time) or you don’t. Pretty straightforward.

As you’ll see in Chapter 4 (if you’re not there already) with Delegation, sometimes you’ll write methods in Cocoa that YOU don’t even call; a framework may call them for you. It doesn’t even have to be the framework; you can write the same kind of thing yourself.

Page 85 (2nd ed.; similar example on page 70 of the 1st ed.) shows an example using @selector to see if an object responds to a given message. But here’s the thing: the message COULD be constructed on the fly from a string (see NSSelectorFromString). A compiler wouldn’t have a prayer of dealing with that.

All that said, there IS a compiler flag for “Unused functions”. Select the project name (at the very top of the nav list), then the target, then Build Settings, and search for Unused. I’m not sure how much use this is, though. When I mistyped the name of a delegate method, it still wasn’t flagged.

A potentially better (if imperfect) strategy is to use a “code coverage” tool that watches what code gets hit and what does not for a given execution. It appears “GCOV” is already installed (on my machine it’s in /usr/bin/gcov), there’s another tool called CoverStory, and this blog post shows how to tie them all together in Xcode 4. I haven’t tried this myself, but it looks like an interesting idea.