I have created a custom property wrapper that writes and reads from a file as its setter and getter.
@propertyWrapper struct Specifier<Value> {
let key: String
let defaultValue: Value
let plistPath: String
var prefs: NSDictionary {
NSDictionary(contentsOfFile: plistPath) ?? NSDictionary()
}
var wrappedValue: Value {
get {
return prefs.value(forKey: key) as? Value ?? defaultValue
}
set {
prefs.setValue(newValue, forKey: key)
prefs.write(toFile: plistPath, atomically: true)
}
}
}
I now want to use this as in a settings view like so:
struct PrefsView: View {
@Specifier<Bool>(key: "enable", defaultValue: true, plistPath: "/Library/path/to/Prefs.plist") private var enable
var body: some View {
Form {
Toggle("Enable", isOn: $enable)
}
}
}
I am given the following error:
Cannot find '$enable' in scope
How might I achieve this effect of writing and reading to a file for setting and getting variable but also allow it to bind to a value?
NOTE: My project does not need to be "App Store Legal"