Skip to content

Commit

Permalink
New init with default parameter that accepts another key
Browse files Browse the repository at this point in the history
  • Loading branch information
Maschina committed Dec 27, 2023
1 parent d8a9f51 commit 21a3f3a
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Sources/Defaults/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,23 @@ extension Defaults {

super.init(name: name, suite: suite)
}

/**
Create a key with another key as default value.
This can be useful if you want to change the name of the key, but still provide support to migrate from the previous key name.
- Parameter name: The name must be ASCII, not start with `@`, and cannot contain a dot (`.`).
*/
@_alwaysEmitIntoClient
public convenience init(
_ name: String,
default legacyKey: Defaults.Key<Value>,
suite: UserDefaults = .standard
) where Value: Defaults.Serializable {
let keyValue = legacyKey.suite[legacyKey]
self.init(name, default: keyValue, suite: suite)
}
}
}

Expand Down

0 comments on commit 21a3f3a

Please sign in to comment.