Problem copying an object (Cocoa) [Solved - needed to implement NSCopying]

Discussion in 'Mac Programming' started by Eraserhead, Sep 28, 2006.

  1. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #1
    I am making a program in XCode (Obj C) and I have created an object in a program which I am going to call MyObject, however when I run this code:
    Code:
    MyObject *newObj;
    newObj=[[MyObject alloc] init];
    //newObj then gets passed to a different method.
    newObj=[newObj copy];
    
    at this point, when the program is running it errors giving this error twice:

    2006-09-28 20:13:51.103 MyProgram[1333] *** -[MyObject copyWithZone:]: selector not recognized [self = 0x5f03d80]
    2006-09-28 20:13:51.116 MyProgram[1333] *** -[MyObject copyWithZone:]: selector not recognized [self = 0x5f03d80]


    (where 0x5f03d80 is the object I am trying to copy.)

    What is going wrong and what do I need to do to make it work?
     
  2. Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #2
    Your class needs to implement the NSCopying protocol.

    From the NSCopying documentation:

    And the copyWithZone: documentation:

     
  3. thread starter macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #3
    Thanks, I've just found that documentation, good to know I'm on the right track!
     
  4. thread starter macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #4
    My program works too! Excellent! :D
     

Share This Page