Git Product home page Git Product logo

defaultskit's Introduction

DefaultsKit

cocoapods compatible carthage compatible language swift

简体中文

DefaultsKit leverages Swift 4's powerful Codable capabilities to provide a Simple and Strongly Typed wrapper on top of UserDefaults. It uses less than 70 lines of code to acomplish this.

Installation >> instructions <<

Usage

Instantiate, or get a shared instance of Defaults

let defaults = Defaults() // or Defaults.shared

Then:

// Define a key
let key = Key<String>("someKey")

// Set a value
defaults.set("Codable FTW 😃", for: key)

// Read the value back
defaults.get(for: key) // Output: Codable FTW 😃

Check if a key has a value:

if defaults.has(key) { 
    // Do your thing
}

If you just need to know that a key/value pair exists, without actually using the value, use the has() method instead of the optional get(for:key). For complex objects it will prevent any unnecessary deserialization.

Implicit Member Expression

You can find a convenience wrapper for your keys by extending DefaultsKey. This allows you use Implicit Member Expression:

// Extend with a custom key
extension DefaultsKey {
    static let someKey = Key<String>("someKey")
}

// Then use it like this
defaults.set("Some key", for: .someKey)
defaults.get(for: .someKey) // Output: Some key

Complex objects

To store a complex object just conform to the Codable protocol:

struct Person: Codable {
    let name: String
    let age: Int
}

Then:

// Create a key
let key = Key<Person>("personKey")

// Get an instance of your Codable conforming enum, struct or class
let person = Person(name: "Bonnie Greenwell", age: 80)

// Set the value
defaults.set(person, for: key)

And finally:

// Read it back
let person = defaults.get(for: key)
person?.name // Bonnie Greenwell
person?.age  // 80

Nested Objects

You can also use nested objects as long as they conform to the Codable protocol:

enum Pet: String, Codable {
    case cat
    case dog
}

struct Person: Codable {
    let name: String
    let pets: [Pet]
}

// Get a Codable conforming instante
let person = Person(name: "Claire", pets: [.cat])

// Set the value
defaults.set(person, for: key)

// And read it back
let person = defaults.get(for: key)
person?.name        // Claire
person?.pets.first  // cat

License

DefaultsKit is released under the MIT license. See LICENSE for details.

Help Wanted

Review/Translate README.zh-CN.md to Chinese

Chinese is the #1 spoken language in the world and I'd love to have DefaultsKit be more inclusive, unfortunately I don't speak Chinese. If you know chinese, and would like to help out, please see issue #1

Thank you 🙏

defaultskit's People

Contributors

cloudnuy avatar hewigovens avatar kmcgill88 avatar nmdias avatar ricsantos avatar wddwycc avatar wuhelong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

defaultskit's Issues

How to read it out from different View Controller?

Dears:

I don't know how to read it back at different view controllers. For example: I save my data at login view controller, and I need to read it out at Friends View Controller. How do I do? Thanks!

Using PLIST encoder for defaults

Hi!

Great idea/framework. Can't wait to use Codable everywhere in my projects.

Looked over the code and thought of something.

  • Why are you using JSONEncoder/Decoder? Since you are storing un UserDefaults, you are converting to JSON data then storing. BUT UserDefaults is a PLIST, so it accepts any value that is a PLIST. You could convert the model you want to save with the PLIST decoder/encoder and not worry about changing back and forth from Data.

Maybe both could be an option. Just curious. 👍

Xcode 12.0.1 & SPM

I get this error when I try to install DefaultsKit with SPM (Carthage works)

because every version of DefaultsKit contains incompatible tools version and root depends on DefaultsKit 0.2.0, version solving failed.

why type of Key class must be generic?

We have to define generic type of every Key instance but I do not get it.

first parameter of 'save' method 'value' already knows what generic type is gonna be saved.

Is it possible to improve Key class which is gonna not be required to define what generic type is?

What I mean:

let object = Person(n: "Mustafa")

Defaults.shared.set(object, Key("admin")

we can get the type of object variable.

Translate README to Chinese

Create a README.CN.md in Chinese.

Chinese is the #1 spoken language in the world and I'd love to have DefaultsKit be more inclusive, unfortunately I don't speak Chinese. If you know Chinese, and would like to help out, I'd be eternally grateful.

Thank you 🙏

Redundant conformance constraint

Great library!

I am trying to write a small wrapper and am running into a compiler warning Redundant conformance constraint . I'm newer to generics but was hoping for some direction what I'm doing wrong. Thanks!

screen shot 2017-10-12 at 1 05 16 pm

Build failed with Xcode14.3

When I tried to build this project with Xcode 14.3, the build failed.
I think this error occurred because the deployment target was not specified

how to initialise default with suitname ?

I have App where I use custom keyboard extension and I want to share default with group identifires for that I need to initialise default with suit name how can we do this with DefaultsKit ?

Test has function on simulator iOS10.3.1 always true

First thanks for sharing this useful frame.

My problem is when I use Xcode 11.5 simulator iOS 10.3.1 test Defaults.shared.has(.userIdKey) function (.userIdKey is String type) after I use clear the value, it's always return true. But when I use Defaults.shared.get(for: .userIdKey) is nil.

So I debug has function source code "po userDefaults.value(forKey: key._key)" is ▿ Optional

  • some : <62706c69 73743030 d4010203 04050608 09582476 65727369 6f6e5824 6f626a65 63747359 24617263 68697665 72542474 6f701200 0186a0a1 0755246e 756c6c5f 100f4e53 4b657965 64417263 68697665 72d10a0b 54726f6f 74800008 111a232d 3237393f 51545900 00000000 00010100 00000000 00000c00 00000000 00000000 00000000 00005b>

(lldb) po userDefaults.object(forKey: key._key)
▿ Optional

  • some : <62706c69 73743030 d4010203 04050608 09582476 65727369 6f6e5824 6f626a65 63747359 24617263 68697665 72542474 6f701200 0186a0a1 0755246e 756c6c5f 100f4e53 4b657965 64417263 68697665 72d10a0b 54726f6f 74800008 111a232d 3237393f 51545900 00000000 00010100 00000000 00000c00 00000000 00000000 00000000 00005b>

I test same code on iOS 11.4, iOS12, iOS13 on simulator are all correct.

I don't know what's make this issue. I have no real machine with version 10.3.1 to test it.

At last I use userDefaults.string(forKey: key._key) test it is correct, It seems userDefaults.value have problem on iOS 10.3.1 version.

May be public func has(_ key: Key) we can get value type first, then use match function get value is better.

memory leak

[Leak Checks]
538.14 KB 85.4% 9255 0x100829463 DefaultsKit
538.14 KB 85.4% 9255 JSONDecoder.decode(_:from:)
279.39 KB 44.3% 4772 +[NSJSONSerialization JSONObjectWithData:options:error:]

DefaultsKit does not handle arguments passed on launch

One very useful feature when debugging is to use Arguments Passed On Launch to override user defaults. It seems DefaultsKit is not able to handle these keys because of the way it attempts to cast to Key.ValueType.

For example, let's say I have the following:

var wipeDatabaseOnLaunch = Key<Bool>("wipeDatabaseOnLaunch")

This works in the normal case:

let value = defaults.get(for: wipeDatabaseOnLaunch)
// value: Optional<Bool>(true)

However, if we pass -wipeDatabaseOnLaunch true argument on launch, the underlying type returned by user defaults is no longer Optional<Bool> but instead Optional<Any>. Since the implementation of get tries to cast to the key's ValueType this cast will fail and you get nil instead.

let value = defaults.get(for: wipeDatabaseOnLaunch)
// value: nil

As a workaround I had to implement a specialized version of get which uses bool(forKey:)

public func get(for key: Key<Bool>) -> Bool {
    return userDefaults.bool(forKey: key._key)
}

Unfortunately, this needs to be done for each of the types you could use as launch arguments. Haven't found a cleaner solution to this.

Missing DefaultsKey

Testing out the repo, looks great! Of course, immediately set out to create default keys, as that just feels right. Noted the Defaults.swift file is missing (that is in the demo).
ps: naming the file to match only makes sense too ;)

Cheers!

Support Implicit Member Expression

Hi @nmdias

Can Key class inherit a class like following, for example?

public class AnyKey {}

public final class Key<ValueType: Codable>: AnyKey {}

This makes it possible to do following.

extension AnyKey {
    static let key = Key<String>("someKey")
}

defaults.set("Codable FTW 😃", for: .key)

My goal is like #9.

Thanks.

Swift.EncodingError.Context(codingPath: [], debugDescription: "Top-level Data encoded as string JSON fragment.", underlyingError: nil)

This was an accident when I converted the custom model using an archive to binary using "Defaults" for storage. This is a problem I recently used with Xcode 9.4.1, Swift 4.0. I didn't see this problem before using Xcode 9.2. The version of Defaults that I am using now is 0.0.8.

I also looked at this document "https://git.snooey.net/Mirrors/swift/blob/a8ba0772cd2f887c2b48549172d2c447140e06d8/stdlib/public/SDK/Foundation/JSONEncoder.swift"
I don't quite understand why this error caused my storage binary type to fail.

I hope you can help me.

Thanks

How to extend Key class Key with keys?

Hello,

Awesome Library!!!

I am trying for the below things:

My Goal
defaults.set(true, forKey: .isUserLoggedIn)

Currently I'm doing in this way:

enum Keys {
    static let isUserLoggedIn = Key<Bool>("isUserLoggedIn")
}

defaults.set(true, forKey: Keys.isUserLoggedIn)

Please guide me.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.