Created
March 23, 2018 17:00
-
-
Save natebirkholz/303fac55ca138afad99ee91c7b22544f to your computer and use it in GitHub Desktop.
Codable serialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
class Person: Codable { | |
let firstName: String | |
let lastName: String | |
let age: Int | |
let birthday: Date | |
init(fName: String, lName: String, ageFor: Int, dob: Date) { | |
firstName = fName | |
lastName = lName | |
age = ageFor | |
birthday = dob | |
} | |
} | |
let dateString = "03-Jun-1970" | |
var dateFormatter = DateFormatter() | |
dateFormatter.dateFormat = "dd-MMM-yyyy" | |
let bDate = dateFormatter.date(from: dateString) | |
let tom = Person(fName: "Tom", lName: "Smith", ageFor: 47, dob: bDate!) | |
guard let url = FileManager.default.urls(for: .applicationSupportDirectory, in: .allDomainsMask).first else { fatalError() } | |
let subdirectory = url.appendingPathComponent("Test") | |
if !FileManager.default.fileExists(atPath: subdirectory.path) { | |
try! FileManager.default.createDirectory(at: subdirectory, withIntermediateDirectories: true, attributes: nil) | |
} | |
let fullURL = subdirectory.appendingPathComponent("SavedModel.mdl") | |
print(fullURL.path) | |
NSKeyedArchiver.archiveRootObject(tom, toFile: fullURL.path) | |
*** Unrecognized selector -[__lldb_expr_3.Person replacementObjectForKeyedArchiver:] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment