I need to read/write properties that are Codable (e.g., Date) and NSCoding (e.g., NSMutableAttributedString) from/to a JSON-formatted file. After looking into how to read from and write to files using Codable, how to do so in the JSON format, and how to combine NSCoding with Codable when some properties don't conform to Codable (but do conform to NSCoding), I kludged together the following code and confused myself in the process.
I finally figured out how to test this, and made changes accordingly. But I'd still like to know how the three decoder/encoder types (NSCoding, Codable, and JSON) interact or substitute for one another.
import Foundation
class Content: Codable {
// Content
var attrStr = NSMutableAttributedString(string: "")
var date: Date?
// Initializer for content
init(attrStr: NSMutableAttributedString, date: Date) {
self.attrStr = attrStr
self.date = date
}
// Need to explicitly define because NSMutableAttributedString isn't codable
enum CodingKeys: String, CodingKey {
case attrStr
case date
}
// Need to explicitly define the decoder. . . .
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
date = try container.decode(Date.self, forKey: .date)
let attrStrData = try container.decode(Data.self, forKey: .attrStr)
attrStr = NSKeyedUnarchiver.unarchiveObject(with: attrStrData) as? NSMutableAttributedString ?? NSMutableAttributedString(string: "Error!")
}
// Need to explicitly define the encoder. . . .
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(date, forKey: .date)
let attrStrData = NSKeyedArchiver.archivedData(withRootObject: attrStr)
try container.encode(attrStrData, forKey: .attrStr)
}
static func getFileURL() -> URL {
// Get the directory for the file
let docsDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
// Get the full path and filename
return docsDir.appendingPathComponent("contentArray").appendingPathExtension("cntnt")
}
static func saveToFile(content: [Content]) {
// Get the file's URL
let fileURL = getFileURL()
do {
// Encode the data
let data = try JSONEncoder().encode(content)
// Write to a/the file
try data.write(to: fileURL)
} catch {
print("Could not encode or save to the file!")
}
}
static func loadFromFile() -> [Content] {
// Get the file's URL
let fileURL = getFileURL()
do {
// Read from the file
let data = try Data(contentsOf: fileURL)
// Decode the data
return try JSONDecoder().decode([Content].self, from: data)
} catch {
print("Could not decode or read from the file!")
return []
}
}
}