"archivedDataWithRootObject:" failing to cause "encodeWithCoder:" on its argument

Discussion in 'Mac Programming' started by NeonKingKong, Feb 11, 2010.

  1. NeonKingKong macrumors member

    NeonKingKong

    Joined:
    Nov 14, 2008
    Location:
    Portland, OR
    #1
    Hello,
    I have an document based application made up of a custom "app controller", myDocument, and a custom "modeling" object. Most of the code is in the appcontroller class, which is instantiated inside InterfaceBuilder and connects to the UI and handles their actions, updates the UI and manipulates it's own modeling system.

    The custom data object as well as my custom controller confirm to NSCoding. I searched this website earlier and thought it was maybe because I was trying to archive NSMutableDictionaries, so I replaced that part of the saving by converting them into two synchronous (I hope) NSMutableArrays and archiving those. However, I'm now finding out through NSLog statements that "encodeWithCoder:" on my custom app controller is NOT getting called.

    My document object is connected to my custom controller, which is instantiated inside interface builder, by the way of an IBOutlet. It then calls "archivedDataWithRootObject:" passing my custom controller as its argument. According to my knowledge "encodeWithCoder:" should get called on my custom controller but it's not.

    Any help is greatly appreciated.
     
  2. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #2
    In a document-based application, the subclassed NSDocument object receives a series of calls from the NSDocumentController in order to create or obtain a model, and a series of calls for the archiving of the model. Where you override this series determines how your document will handle the data. You should place your encoding routine in the -dataOfType:error: method override in your document and your decoding method in the -readFromData: ofType:error: method of your myDocument subclass. This is where the archiving and unarchiving methods normally occur if you are using the standard bplist encoding technique.
     
  3. NeonKingKong thread starter macrumors member

    NeonKingKong

    Joined:
    Nov 14, 2008
    Location:
    Portland, OR
    #3
    Thanks very much. That's currently where the methods are located.
    The dataOfType:error: method works and does not report an error, but I know the object isn't coding itself because of NSLogs I've placed in the encodeWithCoder: class which is the argument of dataOfType:error:.
    Worst case senario I could rewrite the application so that the object I'm encoding, responsible for the model, isn't an object instantiated in InterfaceBuilder, I have a hunch that's why it's not working. I didn't split the controller class and the model class. Thank you very much for you're reply.
     
  4. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #4
    Can you draw a path of ownership from the root object to the one you are failing to encode, with encoding methods that all along that path? Also, if any of those objects are your own classes, I would recommend making sure they declare conformance to the NSCoding protocol, just to be safe.
     
  5. NeonKingKong thread starter macrumors member

    NeonKingKong

    Joined:
    Nov 14, 2008
    Location:
    Portland, OR
    #5
    Thanks. It had something to do with I was archiving a root object that was an interface builder object. I rewrote it to completely seperate myDocument and the data, or to be archived root object, and it worked like a charm.
     

Share This Page