Conforming to NSCoding


This is probably a daft question but I’m unsure why we declare that Possession conforms to NSCoding (p 260) rather than PossessionStore. It seems to be PossessionStore which does all the archiving, not Possession.

Can anyone explain this to me please as I really don’t get it? Many thanks in advance for any help


Because the PossessionStore object isn’t really archiving itself. It does have the -saveChanges and -loadPossessionsIfNecessary methods, but these really just tell our NSMutableArray of Possession objects to archive itself.

Note that our PossessionStore -saveChanges method passes allPossessions (our NSMutableArray) to the NSKeyedArchive class:

- (BOOL)saveChanges { // return success or failure return [NSKeyedArchiver archiveRootObject:allPossessions toFile:[self possessionArchivePath]]; }

Our allPossessions NSMutableArray knows how to archive itself, and during that process it tells each object it holds to archive itself. Those objects are Possession objects - so it’s our Possession class that needs to conform to the NSCoding protocol so it’s “archivable”.