UserDefaults

class UserDefaults : NSObject
  • Mechanica

    Returns true if key exists.

    Declaration

    Swift

    public final func hasKey(_ key: String) -> Bool
  • Mechanica

    Removes all keys and values from user defaults.

    Note

    This method only removes keys on the receiver UserDefaults object. System-defined keys will still be present afterwards. resetStandardUserDefaults simply resets the in-memory user defaults object.

    Declaration

    Swift

    public final func removeAll()
  • Mechanica

    Declaration

    Swift

    public final func optionalInteger(forKey defaultName: String) -> Int?

    Parameters

    defaultName

    A key in the current user’s defaults database.

    Return Value

    The integer value associated with the specified key. If the specified key does not exist, this method returns nil.

  • Mechanica

    Declaration

    Swift

    public final func optionalFloat(forKey defaultName: String) -> Float?

    Parameters

    defaultName

    A key in the current user’s defaults database.

    Return Value

    The floating-point value associated with the specified key. If the key does not exist, this method returns nil.

  • Mechanica

    Declaration

    Swift

    public final func optionalDouble(forKey defaultName: String) -> Double?

    Parameters

    defaultName

    A key in the current user’s defaults database.

    Return Value

    The double value associated with the specified key. If the specified key does not exist, this method returns nil.

  • Mechanica

    Declaration

    Swift

    public final func optionalBool(forKey defaultName: String) -> Bool?

    Parameters

    defaultName

    A key in the current user’s defaults database.

    Return Value

    The bool value associated with the specified key. If the specified key does not exist, this method returns nil.

  • Mechanica

    Returns the object conformig to Codable associated with the specified key, or nil if the key was not found.

    Declaration

    Swift

    public final func codableValue<T: Codable>(forKey defaultName: String) -> T?

    Parameters

    defaultName

    A key in the current user’s defaults database.

  • Mechanica

    Stores an object conformig to Codable (or removes the value if nil is passed as the value) for the provided key.

    Throws

    An error if any value throws an error during encoding.

    Declaration

    Swift

    public final func set<T: Codable>(codableValue value: T?, forKey defaultName: String) throws

    Parameters

    value

    The object to store in the defaults database.

    defaultName

    The key with which to associate with the value.