UserDefaultsStore

open class UserDefaultsStore<T> where T : Decodable, T : Encodable, T : Identifiable

UserDefaultsStore offers a convenient way to store a collection of Codable objects in UserDefaults.

  • Store’s unique identifier.

    Warning: Never use the same identifier for two -or more- different stores.

    Declaration

    Swift

    public let uniqueIdentifier: String
  • JSON encoder to be used for encoding objects to be stored.

    Declaration

    Swift

    open var encoder: JSONEncoder
  • JSON decoder to be used to decode stored objects.

    Declaration

    Swift

    open var decoder: JSONDecoder
  • Initialize store with given identifier.

    Warning: Never use the same identifier for two -or more- different stores.

    Declaration

    Swift

    required public init?(
        uniqueIdentifier: String,
        encoder: JSONEncoder = .init(),
        decoder: JSONDecoder = .init()
    )

    Parameters

    uniqueIdentifier

    store’s unique identifier.

    encoder

    JSON encoder to be used for encoding objects to be stored. default is JSONEncoder()

    decoder

    JSON decoder to be used to decode stored objects. default is JSONDecoder()

  • Save object to store. O(1)

    Throws

    JSON encoding error.

    Declaration

    Swift

    public func save(_ object: T) throws

    Parameters

    object

    object to save.

  • Save optional object (if not nil) to store. O(1)

    Throws

    JSON encoding error.

    Declaration

    Swift

    public func save(_ optionalObject: T?) throws

    Parameters

    optionalObject

    optional object to save.

  • Save array of m objects to store. O(m)

    Throws

    JSON encoding error.

    Declaration

    Swift

    public func save(_ objects: [T]) throws

    Parameters

    objects

    object to save.

  • Get object from store by its id. O(1)

    Declaration

    Swift

    public func object(withId id: T.ID) -> T?

    Parameters

    id

    object id.

    Return Value

    optional object.

  • Get array of objects from store for array of m id values. O(m)

    Declaration

    Swift

    public func objects(withIds ids: [T.ID]) -> [T]

    Parameters

    ids

    array of ids.

    Return Value

    array of objects with the given ids.

  • Get all objects from store. O(n)

    Declaration

    Swift

    public func allObjects() -> [T]

    Return Value

    array of all objects in store.

  • Delete object by its id from store. O(1)

    Declaration

    Swift

    public func delete(withId id: T.ID)

    Parameters

    id

    object id.

  • Delete objects with ids from given m ids array. O(m)

    Declaration

    Swift

    public func delete(withIds ids: [T.ID])

    Parameters

    ids

    array of ids.

  • Delete all objects in store. O(1)

    Declaration

    Swift

    public func deleteAll()
  • Count of all objects in store. O(1)

    Declaration

    Swift

    public var objectsCount: Int { get }
  • Check if store has object with given id. O(1)

    Declaration

    Swift

    public func hasObject(withId id: T.ID) -> Bool

    Parameters

    id

    object id to check for.

    Return Value

    true if the store has an object with the given id.

  • Iterate over all objects in store. O(n)

    Declaration

    Swift

    public func forEach(_ object: (T) -> Void)

    Parameters

    object

    iteration block.