NSKeyedUnarchiver
A decoder that restores data from an archive referenced by keys.
Declaration
class NSKeyedUnarchiverOverview
NSKeyedUnarchiver is a concrete subclass of NSCoder that defines methods for decoding a set of named objects (and scalar values) from a keyed archive. The NSKeyedArchiver class produces archives that this class can decode.
The archiver creates keyed archive as a hierarchy of objects. The archiver treats each object as a namespace into which it can encode other objects. This means that an unarchiver can only decode objects encoded within the immediate scope of their parent object. Objects encoded elsewhere in the hierarchy — whether higher than, lower than, or parallel to this particular object — aren’t accessible. In this way, the keys used by a particular object to encode its instance variables need to be unique only within the scope of that object.
If you invoke one of the decode-prefixed methods of this class using a key that does not exist in the archive, the return value indicates failure. This value varies by decoded type. For example, if a key does not exist in an archive, decodeBool(forKey:) returns false, decodeIntForKey: returns 0, and decodeObject(forKey:) returns nil.
NSKeyedUnarchiver supports limited type coercion for numeric types. You can use any of the integer decode methods to decode a value encoded as any type of integer, whether a standard Int or an explicit 32-bit or 64-bit integer. Likewise, you can use the Float- or Double-returning decode methods to handle value encoded as a Float or Double. If an encoded value is too large to fit within the coerced type, the decoding method throws a rangeException. Further, when trying to coerce a value to an incompatible type — for example decoding an Int as a Float — the decoding method throws an invalidUnarchiveOperationException.
Topics
Creating a Keyed Unarchiver
Unarchiving Data
unarchiveTopLevelObjectWithData(_:)unarchivedObject(ofClass:from:)unarchivedObject(ofClasses:from:)unarchivedObject(ofClasses:from:)requiresSecureCodingunarchiveObject(with:)unarchiveObject(withFile:)
Decoding Data
containsValue(forKey:)decodeDecodable(_:forKey:)decodeTopLevelDecodable(_:forKey:)decodeBool(forKey:)decodeBytes(forKey:returnedLength:)decodeDouble(forKey:)decodeFloat(forKey:)decodeInt32(forKey:)decodeInt64(forKey:)decodeObject(forKey:)finishDecoding()decodingFailurePolicy