Pointers & Archiving in Cocoa

Member
Posts: 118
Joined: 2002.08
Post: #1
I'm working on a "SimCinema" sort of game, done completely in Cocoa with the standard Aqua interface. I'm adding the option of user's creating a new movie and specifying which of their preexisting movies it is a sequel to. So I can reference it later, I've added a pointer in each "movie" object to another "movie" object that is its prequel.

How would I go about writing the "encodeWithCoder" and "initWithCoder" code for this? I do not want to write the whole "movie" object of the prequel into the coder because it is already being written in as its own movie, and because I just want each movie to have a pointer to its prequel, but not actually contain it.

I hope this makes some sense.
Quote this message in a reply
Member
Posts: 118
Joined: 2002.08
Post: #2
I think what I have to do is just encode the index of the prequel in the "movies" array and use that index to relocate the movie in the "initWithCoder" function. The issue there is, the whole movies array can't have all been initialized, so I can't really reference it at that point, can I?
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #3
This is done automagically. You go ahead and encode the prequel pointer as well. NSCoder will notice that you're archiving the same object twice, and will just put a token in the stream that references the already-archived prequel. This is also the case with cyclic dependencies, so you can do both without any trouble.
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #4
Fenris Wrote:This is done automagically. You go ahead and encode the prequel pointer as well. NSCoder will notice that you're archiving the same object twice, and will just put a token in the stream that references the already-archived prequel. This is also the case with cyclic dependencies, so you can do both without any trouble.
That's good to know, I didn't know it did such automagical stuff.
Quote this message in a reply
Member
Posts: 118
Joined: 2002.08
Post: #5
Brilliant. Cocoa is the man.
Quote this message in a reply
rhiannon
Unregistered
 
Post: #6
Look up 'conditional' and 'unconditional' encoding to learn how to control this.
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #7
rhiannon Wrote:Look up 'conditional' and 'unconditional' encoding to learn how to control this.
Actually, the fact that objects are only encoded once has nothing to do with them being conditionally encoded or not.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Thread-Safe reference counted smart pointers in c++ JeroMiya 1 3,273 Nov 21, 2006 12:28 PM
Last Post: OneSadCookie
  Should global variables be pointers or full objects? ia3n_g 1 2,406 Aug 4, 2006 05:53 PM
Last Post: OneSadCookie
  Dangling Pointers/ Memory Leak definitions... WhatMeWorry 1 4,040 Oct 3, 2005 10:43 AM
Last Post: OneSadCookie
  Archiving Object blobbo 5 3,383 Jun 2, 2005 06:51 PM
Last Post: Steven
  reading void pointers Duane 16 6,869 May 10, 2005 03:47 PM
Last Post: Puzzler183