DevPrep

All topics › Objective-C

NSCoding, archiving

Middle PRO

Essence

Archiving is serializing an object graph into NSData and back with NSKeyedArchiver/NSKeyedUnarchiver. For a class to be archivable it implements the NSCoding protocol:

NSCoder is the abstract "coder" class; the keyed archiver writes key → value pairs, which lets the set of fields change between versions (old keys are simply ignored, missing new ones make decodeObjectForKey: return nil). This is how UIViews are stored in storyboards/XIBs, state restoration, NSUserDefaults for non-standard types, on-disk caches.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store