Skip to content

Instantly share code, notes, and snippets.

View tadasr's full-sized avatar

Tadas Razmislavicius tadasr

View GitHub Profile
@tadasr
tadasr / CodableStorage.swift
Last active February 7, 2023 14:14
The easiest way to store data model to file using `Codable` and `NSKeyedArchiver`.
import PlaygroundSupport
import Foundation
func load<T: Decodable>(from file: URL) -> T? {
guard let data = NSKeyedUnarchiver.unarchiveObject(withFile: file.path) as? Data else { return nil }
do {
let products = try PropertyListDecoder().decode(T.self, from: data)
print("Successful load")
return products
} catch let e {