ProtocolFoundation5.9.0
NSCoding
The NSCoding
protocol declares the two methods that a class must implement so that instances of that class can be encoded and decoded. This capability provides the basis for archiving (where objects and other structures are stored on disk) and distribution (where objects are copied to different address spaces).
protocol NSCoding
In keeping with object-oriented design principles, an object being encoded or decoded is responsible for encoding and decoding its instance variables. A coder instructs the object to do so by invoking encode(with:)
or init(coder:)
. encode(with:)
instructs the object to encode its instance variables to the coder provided; this method can be invoked any number of times. init(coder:)
instructs the object to initialize itself from data in the coder provided. Any object class that should be codable must adopt the NSCoding protocol and implement its methods.
Requirements
init?(coder: NSCoder
) Initializes an object from data in a given unarchiver.
func encode(with: NSCoder
) Encodes an instance of a conforming class using a given archiver.
Citizens in Foundation
Subtypes
protocol NSSecureCoding
Conforming to the
NSSecureCoding
protocol indicates that an object handles encoding and decoding instances of itself in a manner that is robust against object substitution attacks.