2

According to Apple's documentation, Archives convert data into architecture-independent byte streams. What is the difference between serializing data (like saving values to a property list) and archiving it?

Can archives be created with or without NSCoder? Is the NSCoder protocol only used to archive custom objects?

I understand that archives are a way to save object graph relationships and maintain object mutability - am I correct in thinking that for saving an object graph with custom objects, I would need to encode those objects into NSData, archive them to a byte stream to maintain the graph relationship, and then have the option of saving to the defaults database with NSUserDefaults or saving to disk?

Additionally, what's the difference between the defaults database and saving to disk?

I just want to get a better understanding of how the terms all relate to one another.

I've laid out my general idea of how these things interrelate like so:

I THINK -

NSKeyedArchiver is used to encode and store an object graph as a byte stream. It traverses the object graph, maintaining relationships, and calls the encoding protocol methods on each object. NSKeyedArchiver just keeps track of the object graph and saves it - we can either save it to disk or we can save to NSUserDefaults (which is a property list). *it is advisable to archive data to a file directory on disk instead of NSUserDefaults.

Objects can be serialized to a property list (or to NSUserDefaults) without encoding them as long as they are arrays, strings, integers, etc.

Let’s say we have an array (which is automatically NSKeyedArchiver and PropertyListSerializer compliant) and it’s full of custom objects. Those custom objects must implement the NSCoder protocol methods. Then, somewhere else, we can create an instance of NSData that is equal to an NSKeyedArchive using that array, and save that instance of NSData to our desired location.

What happens is, the archiver traverses over the contents of that array, implementing the NSCoder protocol methods that each object adheres to. It keeps track of relationships in the object (the array), persisting the object graph.

When we want that data back, we can go into the file we saved to, check its contents, and create an instance of NSData. We make that instance equal to the dataWithContentsOfFile of the path specified. Then, we create an array and unpackaged the NSData into that array by called the NSKeyedUnarchiver unarchiveObjectWithData method. The archiver traverses over each object in the NSData instance and calls initWithCoder, essentially deserializing the byte stream.

Places we can possible store objects - NSUserDefaults Disk (file path)

Ways to store custom objects - NSKeyedArchiver, which implements NSCoder protocol methods to convert custom objects into a byte stream that represents an object graph.

What must we use to store custom objects? NSCoder protocol methods.

karan satia
  • 307
  • 4
  • 16
  • All of this falls under the label persistence. There are many ways to do that depending on your needs. Archiving is the most general of the ways you mention and can be customised to your needs. User defaults is sort of built on top of that and provides a simpler API for storing user config data. Not suitable to store for example word processor documents in user defaults. Another easier API for persistence is CoreData. I think both user defaults and coreData are built on top of the archiving API. Today if you want to create user movable files I believe coreData is your tool in most instances – pco494 Feb 03 '16 at 23:56
  • @pco494 actually you don't need to archive to use user defaults. You can store certain data types directly without preserving the object graph (you can just store the object hierarchy), bypassing the need to encode and decode data. Additionally, you can archive to a data object (NSData), but if you're just storing a string, array, int, etc. you don't need to archive or encode anything. You can just store in NSUserDefaults and bypass all of that. – karan satia Feb 04 '16 at 01:53
  • I'm sorry I was unclear. That was my point exactly. NSUserDefaults hides the NSCoding thing but I believe it is still used internally by NSUserDefaults, but I may be wrong. – pco494 Feb 06 '16 at 03:53
  • more explanation is required. – Aaban Tariq Murtaza Apr 26 '22 at 16:44

0 Answers0