Skip to content

ky1vstar/SwiftyUserDefaultsNamespaces

Repository files navigation

SwiftyUserDefaultsNamespaces

Platforms CI CocoaPods compatible Carthage compatible SPM compatible

This is an extension for SwiftyUserDefaults which provides support for namespaces (in fact nested KeyStores). SwiftyUserDefaultsNamespaces is designed to separate preferences in parts which then can be supplied to according components in your project.

Usage

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

Installation

CocoaPods

SwiftyUserDefaultsNamespaces is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SwiftyUserDefaultsNamespaces'

Carthage

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

Swift Package Manager

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.

License

SwiftyUserDefaultsNamespaces is available under the MIT license. See the LICENSE file for more info.

About

Handy SwiftyUserDefaults extension which provides namespaces support

Resources

License

Stars

Watchers

Forks