🏗️
透過 NSKeyedArchiver 儲存 Struct 物件
To encode and to decode an object with NSKeyedArchiver and NSKeyedUnarchiver, the class must conforms NSCoding but that is what Swift struct s can't do.
PropertyListEncoder
In order to achive this, we have to use PropertyListEncoder to encode instances of Swift struct s then passing the output to NSKeyedArchiver.
struct has to conforms to Encodable to able to be encoded by PropertyListEncoder.
PropertyListDecoder
To unarchive, decoding data object from NSKeyedUnarchiver, then decoding back to struct objects with PropertyListDecoder.
struct has to conforms to Decodable to able to be decoded by PropertyListDecoder.
Code
Codable Struct
So the struct should conforms to Codable
, which is a Decodable & Encodable
combination. Encodable
is for encoding, Decodable
is for decoding.
struct Book: Codable {
let title: String
}
NSKeyedArchiver - Encode
let book = Book(title: "Harry Potter")
if let encoded = try? PropertyListEncoder().encode(book) {
let archiver = NSKeyedArchiver(requiringSecureCoding: false)
// Encode with a key
archiver.encode(encoded, forKey: "model")
// End encoding stream
archiver.finishEncoding()
// Export the encoded data and store it to somewhere
// For example a `var data: Data?`
self.data = archiver.encodedData
}
NSKeyedUnarchiver - Decode
if let data = data,
let unarchiver = try? NSKeyedUnarchiver(forReadingFrom: data) {
if let data = unarchiver.decodeObject(of: NSData.self, forKey: "model") as? Data,
let book = try? PropertyListDecoder().decode(Book.self, from: data) {
print(book)
}
}
Discussion