Skip to content

Instantly share code, notes, and snippets.

@stleamist
Created June 16, 2020 09:22
Show Gist options
  • Save stleamist/ce26bb455a47d7329f75ef0c4105b32f to your computer and use it in GitHub Desktop.
Save stleamist/ce26bb455a47d7329f75ef0c4105b32f to your computer and use it in GitHub Desktop.
import Disk
@propertyWrapper
struct File<Value: Codable> {
let path: String
let directory: Disk.Directory
let defaultValue: Value
init(_ path: String, directory: Disk.Directory, defaultValue: Value) {
self.path = path
self.directory = directory
self.defaultValue = defaultValue
}
var wrappedValue: Value {
get { return (try? Disk.retrieve(path, from: directory, as: Value.self)) ?? defaultValue }
set { try? Disk.save(newValue, to: directory, as: path) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment