θ Grunberg
θ Grunberg

Reputation: 3

Save a struct with NSCoding

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 ?

Upvotes: 0

Views: 716

Answers (1)

Sameh
Sameh

Reputation: 456

You better use codable if you can make book a struct .

struct Book: Codable {

var title: String
var chapter: [Chapter]

struct Chapter:Codable {
    var chapTitle: String
    var texte: [Texte]
}

struct Texte:Codable {
    var description: String
    var texte: [String]
    var position: [Int]
    var hidden: [Int?]
}

Save

static func saveData (books: [Book]) throws
{
     let encoded = try JSONEncoder().encode(books)
     try encoded.write(to: ArchiveURL)
     print("Books saved")
}

Retrive

//retrive data from it's saved place
static func loadData() throws -> [Book]
{
     let data = try Data(contentsOf: ArchiveURL)
     return try JSONDecoder().decode([Book].self, from: data)
}

Also edit the variables on top

let directory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] + "/learn/"

let ArchiveURL = URL(fileURLWithPath: directory)

Upvotes: 0

Related Questions