• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Archiving objects without their owner[1]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Archiving objects without their owner[1]


  • Subject: Re: Archiving objects without their owner[1]
  • From: Chris Kane <email@hidden>
  • Date: Thu, 20 Dec 2001 14:03:02 -0800

On Tuesday, December 18, 2001, at 01:19 PM, Dan Bernstein wrote:
What I'm trying to do is basically what (I think) AppKit does with nib files:

I want to archive an object graph in which multiple objects point to some "owner" object. I don't want the owner (and the graph rooted at it) to be archived. When unarchiving, I want everything that pointed to the "owner" to point to an object I supply.

I guess I should use [NSArchiver replaceObject:withObject:] to archive some "dummy" "placeholder" instead of the owner when archiving. It's just that it isn't clear when I'm supposed to do when unarchiving, in particular - what should be the first argument to [NSUnarchiver replaceObject:withObject:].

The way to hook this into the archiving framework is to implement -replacementObjectForArchiver: on the owner object class to return an instance of a placeholder type class you create for this purpose.

Obviously, do NOT put a -replacementObjectForArchiver: method on any class which you do not own, since you will change the archiving behavior for the whole class and subclasses. The assumption here is that the "owner" is a class you've created, which never needs to be archived normally.

In the -encodeWithCoder: of the placeholder class, you don't need to encode anything, probably. In the -initWithCoder: method, release self and return an instance of the owner class you want on unarchive.

The only "trick" then is that this placeholder class has to have a way to get a hold of an instance, the SAME instance, of the new owner class, every time the placeholder's -initWithCoder: is called. This could be done by having the placeholder class keep track of one in a static variable (possibly with some way to reset that variable to nil, if you need to unarchive several times with different owners). Or a typical way is to put a +[FooOwner +sharedFooOwner] method on the owner class (FooOwner) which keeps a singleton.



Chris Kane
Cocoa Frameworks, Apple


  • Follow-Ups:
    • Re: Archiving objects without their owner[1]
      • From: Dan Bernstein <email@hidden>
References: 
 >Archiving objects without their owner[1] (From: Dan Bernstein <email@hidden>)

  • Prev by Date: Re: CriticalAlert
  • Next by Date: Re: CriticalAlert
  • Previous by thread: Re: Archiving objects without their owner[1]
  • Next by thread: Re: Archiving objects without their owner[1]
  • Index(es):
    • Date
    • Thread