View Full Version : copyWithZone: in Cocoa Design Patterns book

Oct 22, 2009, 03:44 PM
I am reading Cocoa Design Patterns and I like it so far, but on page 141 I found the first thing that I think is wrong and I would not give it a second thought if there wasn't the same thing on page 143 in the method -mutableCopyWithZone: and now I'm not sure if it's wrong at all or if I'm missing something.

Errata for the book (http://cocoadesignpatterns.squarespace.com/errata/) is missing in action and gives me: "The journal that this archive was targeting has been deleted. Please update your configuration."

- (id)copyWithZone:(NSZone *)aZone
id result = [[[self class] allocWithZone:aZone]
initWithWord:[self word] clue:[self clue]];

[[result puzzleSpecificAttributes] addEntriesFromDictionary:
[self puzzleSpecificAttributes]];

return self; // THIS!

Returning self is wrong?

Yet, that is the first issue I found with this book. Everything before page 141 was so perfect, and now they made the same mistake twice (probably due to copy/paste editing) and of all places in the chapter about copying! ;)

Oct 22, 2009, 08:35 PM
That is definitely wrong. If you want to reassure yourself, run the static analyzer on that code.

Oct 23, 2009, 03:25 AM

unfortunately they don't give a full project for this chapter, but I can put someting similar into one of my own projects and test it there.

Erik M. Buck
Oct 29, 2009, 05:08 PM
Thank you for the correction. The sample is in fact wrong. The method should return result. I will update the errata page for the book as soon as I figure out why it is broken.

Oct 30, 2009, 03:49 PM
Thank YOU for the book.

And if I may take the opportunity, does Cocoa Programming Developer's Handbook (http://www.amazon.com/Cocoa-Programming-Developers-Handbook-Chisnall/dp/0321639634) have anything to do with Cocoa Programming (http://www.amazon.com/Cocoa-Programming-Scott-Anguish/dp/0672322307)? Description on Amazon says "2nd edition" for this book by David Chisnall.

If it hasn't, is there any chance for the 2nd edition of original Cocoa Programming book?