RVS_PersistentPrefs

open class RVS_PersistentPrefs : NSObject

This class is meant 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).

Private Properties

  • This is a class cache. The reason for this, is to reduce the number of times the prefs are loaded in. It’s a memory-intensive process. The first key is the general prefs key, and the second key is each prefs key.

    Declaration

    Swift

    private static var _cache: [String : [String : Any]]
  • 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]

Private Methods

  • 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

Public Enums

  • Errors that Can be thrown from methods in this class

    See more

    Declaration

    Swift

    public enum PrefsError : Error

Public Static Properties

  • This is used to specify an App Group ID, if the prefs are shared within an app.

    By default, this is nil, so the standard UserDefaults are used. However, if there is a Group ID, the UserDefaults for that group are used.

    Declaration

    Swift

    public static var groupID: String?

Public Properties

  • key

    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

    open var key: String
  • This is any error that was thrown during a save or a load. May be nil.

    Declaration

    Swift

    open var lastError: PrefsError?

Public Calculated Properties

  • This returns whichever UserDefaults we are using. It will return standard, if there are no UserDefaults for the Group ID.

    Declaration

    Swift

    open var userDefaults: UserDefaults? { get }
  • The number of stored values.

    Declaration

    Swift

    open var count: Int { get }
  • 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

    open 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 }

Public Subscript

  • This is a direct-access subscript that allows you to use the prefs object to reach its stored properties.

    Declaration

    Swift

    public subscript(inKey: String) -> Any? { get set }

Initializers

  • 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.

    Declaration

    Swift

    public init(key inKey: String! = nil, values inValues: [String : Any]! = [:])

    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.

Public Methods

  • This clears the values, and flushes the cache. It then reloads the values.

    Declaration

    Swift

    public func flush()
  • This clears the values, and flushes the cache, but does not load.

    Declaration

    Swift

    public func clear()