Skip to content

Instantly share code, notes, and snippets.

@natebirkholz
Created March 23, 2018 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natebirkholz/303fac55ca138afad99ee91c7b22544f to your computer and use it in GitHub Desktop.
Save natebirkholz/303fac55ca138afad99ee91c7b22544f to your computer and use it in GitHub Desktop.
Codable serialization
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