All Projects → Nirma → Default

Nirma / Default

Licence: mit
Modern interface to UserDefaults + Codable support

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Default

Heimdall
Self-hosted personal email guardian with one-step deployment
Stars: ✭ 459 (-1.5%)
Mutual labels:  hacktoberfest
Bot
The community bot for the Python Discord community
Stars: ✭ 460 (-1.29%)
Mutual labels:  hacktoberfest
Px4 Autopilot
PX4 Autopilot Software
Stars: ✭ 5,090 (+992.27%)
Mutual labels:  hacktoberfest
Node I18n Iso Countries
i18n for ISO 3166-1 country codes
Stars: ✭ 461 (-1.07%)
Mutual labels:  hacktoberfest
Example Ios Apps
 A curated list of Open Source example iOS apps developed in Swift. An amazing list for people who are beginners and learning ios development and for ios developers who need any example app or feature.
Stars: ✭ 461 (-1.07%)
Mutual labels:  hacktoberfest
Polaris
A cross-platform, minimalist web framework for PowerShell
Stars: ✭ 464 (-0.43%)
Mutual labels:  hacktoberfest
Skript
Skript is a Bukkit plugin which allows server admins to customize their server easily, but without the hassle of programming a plugin or asking/paying someone to program a plugin for them.
Stars: ✭ 458 (-1.72%)
Mutual labels:  hacktoberfest
Wp Cli
⚙️ WP-CLI framework
Stars: ✭ 4,474 (+860.09%)
Mutual labels:  hacktoberfest
Puppet Nginx
Puppet Module to manage NGINX on various UNIXes
Stars: ✭ 462 (-0.86%)
Mutual labels:  hacktoberfest
Typescript Language Server
TypeScript & JavaScript Language Server
Stars: ✭ 462 (-0.86%)
Mutual labels:  hacktoberfest
Web App
Dictionary database with future API and bot integrations
Stars: ✭ 461 (-1.07%)
Mutual labels:  hacktoberfest
Net6 Mobile Samples
Xamarin .NET 6 *early* preview. Not for production use.
Stars: ✭ 445 (-4.51%)
Mutual labels:  hacktoberfest
Viaversion
Allows the connection of newer clients to older server versions for Minecraft servers.
Stars: ✭ 463 (-0.64%)
Mutual labels:  hacktoberfest
Ruby Next
Ruby Next makes modern Ruby code run in older versions and alternative implementations
Stars: ✭ 460 (-1.29%)
Mutual labels:  hacktoberfest
Thelounge
💬 ‎ Modern, responsive, cross-platform, self-hosted web IRC client
Stars: ✭ 4,618 (+890.99%)
Mutual labels:  hacktoberfest
Wordpresscan
WPScan rewritten in Python + some WPSeku ideas
Stars: ✭ 456 (-2.15%)
Mutual labels:  hacktoberfest
Poliastro
poliastro - 🚀 Astrodynamics in Python
Stars: ✭ 462 (-0.86%)
Mutual labels:  hacktoberfest
Betaflight
Open Source Flight Controller Firmware
Stars: ✭ 4,986 (+969.96%)
Mutual labels:  hacktoberfest
Laravel Medialibrary
Associate files with Eloquent models
Stars: ✭ 4,743 (+917.81%)
Mutual labels:  hacktoberfest
Json Viewer
A JSON viewer plugin for Notepad++. Displays the selected JSON string in a tree view.
Stars: ✭ 464 (-0.43%)
Mutual labels:  hacktoberfest

Default

Build Status platforms Swift 5.0 CocoaPods compatible Carthage compatible License

Modern interface to UserDefaults + Codable support

What is Default?

Default is a library that extends what UserDefaults can do by providing extensions for saving custom objects that conform to Codable and also providing a new interface to UserDefaults described below, via the protocol DefaultStorable. You can use only the Codable support extensions or the DefaultStorable protocol extensions or both. (or none, that's cool too)

Features

  • [x] Read and write custom objects directly to UserDefaults that conform to Codable
  • [x] Provides an alternative API to UserDefaults with DefaultStorable

Don't see a feature you need?

Feel free to open an Issue requesting the feature you want or send over a pull request!

Why default?

This library has Storing keys and values in defaults the normal way is error prone because typing out the string value for a key every time leaves the possibility of mistyped keys and keeping track of which keys are used and what is currently stored in UserDefaults is somewhat hard. Defining objects specifically for storing in user defaults makes the job of keeping track of what is currently being stored in UserDefaults as simple as searching the project's source code for instances that conform to DefaultStorable. Using objects specifically for storing a set of data in UserDefaults allows settings for a certain piece of data to be logically grouped together.

Usage

DefaultStorable - A better way of interacting with UserDefaults

Instead of manually adding key values to the key store or having to implement NSCoding manually and bloating up object code, you can simply and clearly define defaults objects with a clear intent of being used as a means of storing defaults.

Much like how conforming to Codable gets you a lot for free, so does conforming to DefaultStorable. **The object conforming to DefaultStorable must also conform to Codable:

Say we want to store theme settings in UserDefaults (fair enough right?) we first define our object conforming to Codable and DefaultStorable.

Define object conforming to DefaultStorable

struct VisualSettings: Codable, DefaultStorable {
    let themeName: String
    let backgroundImageURL: URL?
}

Create & Save the object to UserDefaults

let settings = VisualSettings(themeName: "bright", backgroundImageURL: URL(string: "https://..."))
settings.write()

If you need to save the data under a different key other than the default key (the type name, in this case "VisualSettings") then this can be achieved by providing the optional argument to write(withKey:):

let settings = VisualSettings(themeName: "bright", backgroundImageURL: URL(string: "https://..."))
settings.write(withKey: "someUniqueKey")

Read it back later when ya need it!

if let settings = VisualSettings.read() {
    // Do something
}

If you saved the default under a unque key then it can be read back by providing the optional argument to read(forKey:):

if let settings = VisualSettings.read(forKey: "someUniqueKey") {
    // Do something
}

Swift 4 Codable Support

This library offers support for directly storing custom objects within UserDefaults that conform to Codable. With the release of Swift 4 comes the Codable protocol, which provides support for serializing objects. UserDefaults has not been updated to work with Swift 4's Codable protocol so if saving custom objects directly to UserDefaults is necessary then that object must support NSCoding and inherit from NSObject.

// 1: Declare object (just conform to Codable, getting default encoder / decoder implementation for free)
struct VolumeSetting: Codable {
    let sourceName: String
    let value: Double
}

let setting = VolumeSetting(sourceName: "Super Expensive Headphone Amp", value: 0.4)
let key: String = String(describing: VolumeSetting.self)

// 2: Write
UserDefaults.standard.df.store(setting, forKey: key)

// 3: Read
UserDefaults.standard.df.fetch(forKey: key, type: VolumeSetting.self)

Customization

If the default behaviour of Default does not quite fit your needs, then any of the default implementation details can be overridden.

The most commonly overridden properties are defaultIdentifier and defaults.

defaultIdentifier

defaultIdentifier is the key by which your object will be stored. This defaults to the type name of the object being stored.

  public static var defaultIdentifier: String {
        return String(describing: type(of: self))
    }

defaults

defaults will return the UserDefaults database that your application will store defaults objects in. The default implementation returns UserDefaults.standard

    public static var defaults: UserDefaults {
        return UserDefaults.standard
    }

How does this library work?

UserDefaults requires custom types to confrom to NSCoding and be a subclass of NSObject. Doing that is a little time consuming, and conforming to NSCoding requires implementing Decoding / Encoding methods that take a bit of code to implement. The good news is that Data conforms to NSCoding so if you can find a way to convert your object to Data then you can store it in UserDefaults, The Codable protocol in Swift 4 does just that! This library takes advantage of the Codable protocol introduced in Swift 4. The way it works is by taking an object that conforms to Codable and encoding it into a Data object which can then be stored in UserDefaults, then when you want to read it back out again just convert using Codable again!

It's that Simple!

Installation

Carthage

If you use Carthage to manage your dependencies, simply add Default to your Cartfile:

github "Nirma/Default"

If you use Carthage to build your dependencies, make sure you have added Default.framework to the "Linked Frameworks and Libraries" section of your target, and have included Default.framework in your Carthage framework copying build phase.

CocoaPods

If you use CocoaPods to manage your dependencies, simply add Default to your Podfile:

pod 'Default'

Requirements

  • Xcode 9.0
  • Swift 4.0+

Contribution

Contributions are more than welcome!

License

Default is free software, and may be redistributed under the terms specified in the LICENSE file.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].