I want to save the following data:
class Book: NSObject, NSCoding {
var title: String
var chapter: [Chapter]
struct Chapter {
var chapTitle: String
var texte: [Texte]
}
struct Texte {
var description: String
var texte: [String]
var position: [Int]
var hidden: [Int?]
}
I am currently using the NSCoder to save the Book class:
static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
static let ArchiveURL = DocumentsDirectory.appendingPathComponent("learn")
struct PropertyKey {
static let title = "title"
static let chapter = "Chapter"
}
//MARK: - Initialization
init?(title: String, chapter: [Book.Chapter]) {
self.title = title
self.chapter = chapter
}
//MARK: - NSCoding
func encode(with aCoder: NSCoder) {
aCoder.encode(title, forKey: PropertyKey.title)
aCoder.encode(chapter, forKey: PropertyKey.chapter)
}
required convenience init?(coder aDecoder: NSCoder) {
let title = aDecoder.decodeObject(forKey: PropertyKey.title) as! String
let chapter = aDecoder.decodeObject(forKey: PropertyKey.chapter) as! [Book.Chapter]
self.init(title: title, chapter: chapter)
}
With this I can save the Book's title
, and an empty array of chapter
.
However when I try to add chapTitle
and texte
to a Book object, I get the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x60000066dec0'
Does this mean that the struct could not be saved ? What should I change ?