Created
June 16, 2020 09:22
-
-
Save stleamist/ce26bb455a47d7329f75ef0c4105b32f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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