copyWithZone: in Cocoa Design Patterns book

Discussion in 'Mac Programming' started by idelovski, Oct 22, 2009.

  1. macrumors regular

    Joined:
    Sep 11, 2008
    #1
    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 is missing in action and gives me: "The journal that this archive was targeting has been deleted. Please update your configuration."

    Code:
    - (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! ;)
     
  2. macrumors 6502

    Joined:
    Jul 25, 2006
    #2
    That is definitely wrong. If you want to reassure yourself, run the static analyzer on that code.
     
  3. thread starter macrumors regular

    Joined:
    Sep 11, 2008
    #3
    Thanks,

    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.
     
  4. macrumors newbie

    Joined:
    Oct 29, 2009
    #4
    Thank you for the correction.

    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.
     
  5. thread starter macrumors regular

    Joined:
    Sep 11, 2008
    #5
    Thank YOU for the book.

    And if I may take the opportunity, does Cocoa Programming Developer's Handbook have anything to do with Cocoa Programming? 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?
     

Share This Page