This is an extension for SwiftyUserDefaults which provides support for namespaces (in fact nested KeyStore
s). SwiftyUserDefaultsNamespaces
is designed to separate preferences in parts which then can be supplied to according components in your project.
Define your keys and namespaces!
extension DefaultsKeys {
var launchCount: DefaultsKey<Int> {
.init("launchCount", defaultValue: 0)
}
var player: DefaultsNamespace<PlayerKeyStore> {
.init(namespaceKey: "player", keyStore: .init())
}
}
struct PlayerKeyStore: DefaultsKeyStore {
var volume: DefaultsKey<CGFloat?> {
.init("volume")
}
}
And just use it ;-)
Defaults[\.player][\.volume] = 1
Defaults.userSpecific(for: user)[\.name] = "David"
// or with dynamicMemberLookup
Defaults.player.volume = 1
SwiftyUserDefaultsNamespaces is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SwiftyUserDefaultsNamespaces'
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
To integrate SwiftyUserDefaultsNamespaces into your Xcode project using Carthage, specify it in your Cartfile
:
github "ky1vstar/SwiftyUserDefaultsNamespaces"
Run carthage update
to build the framework and drag the built SwiftyUserDefaultsNamespaces.framework
into your Xcode project.
On your application targets’ “Build Phases” settings tab, click the “+” icon and choose “New Run Script Phase” and add the Framework path as mentioned in Carthage Getting started Step 4, 5 and 6
To integrate using Apple's Swift Package Manager, add the following as a dependency to your Package.swift
:
dependencies: [
.package(url: "https://github.com/ky1vstar/SwiftyUserDefaultsNamespaces.git", from: "1.0.0")
]
Alternatively navigate to your Xcode project, select Swift Packages
and click the +
icon to search for SwiftyUserDefaultsNamespaces
.
SwiftyUserDefaultsNamespaces is available under the MIT license. See the LICENSE file for more info.