RVS_PersistentPrefs
public class RVS_PersistentPrefs : NSObject
This class is designed to act as an abstract
base class, providing a simple Dictionary
datastore that is stored in the UserDefaults
in the Application Bundle. It is designed to be reliable, and extremely simple to use.
The stored Dictionary
would be a String-keyed Dictionary of Any
(flexible types). It s up to subclasses to specialize the typeless data.
THIS IS NOT EFFICIENT OR ROBUST!
It is meant as a simple bucket
for things like application preferences. It is not an industrial data storage solution. You have been warned.
Subclasses could declare their accessors as KVO
-style, thus, providing a direct way to influence persistent state.
You can also directly observe the .values property. It will change when ANY pref is changed (so might not be suitable for pick and choose
observation).
-
This is the Dictionary. It’s private, and shouldn’t need to be accessed outside this base class.
Declaration
Swift
private var _values: [String : Any]
-
This is a private method that saves the current contents of the _values Dictionary to persistent storage, keyed by the value of the
key
property.Throws
An error, if the values are not all codable.Declaration
Swift
private func _save() throws
-
This is a private method that reads in the data from persistent storage for this instance (using the
key
property), and loads the _values Dictionary property.Throws
An error, if there were no stored prefs for the given key.Declaration
Swift
private func _load() throws
-
Errors that Can be thrown from methods in this class
See moreDeclaration
Swift
public enum PrefsError : Error
-
This is the key that is used to store and fetch the collection of data to be used to populate the _values Dictionary.
Declaration
Swift
public var key: String
-
This is any error that was thrown during a save or a load.
Declaration
Swift
public var lastError: RVS_PersistentPrefs.PrefsError!
-
This calculated property MUST be overridden by subclasses. It is an Array of String, containing the keys used to store and retrieve the values from persistent storage.
Declaration
Swift
public var keys: [String] { get }
-
This is an accessor for the _values Dictionary. Before it returns the Dictionary, it loads data from persistent storage. After it sets the Dictionary, it saves the resulting Dictionary to persistent storage. This is meant to make persistent storage completely transparent. You simply read and write the Dictionary, using these accessors. The saving and retrieving happens in the background.
Declaration
Swift
@objc dynamic public var values: [String : Any] { get set }
-
Declaration
Swift
subscript(inKey: String) -> Any? { get set }
-
The default initializer uses the current subclass typename as the main key.
Declaration
Swift
public override init()
-
You can initialize instances with a key and some initial data values.
Parameters
key
Optional (default is nil). A String, with a key to be used to associate the persistent state of this object to storage in the bundle. If not provided, the subclass classname is used as the key.
values
Optional (default is nil). A Dictionary
, with the values to be stored. If not provided, then the instance is populated by any persistent prefs. If provided, then the persistent prefs are updated with the new values.