Code Monkey home page Code Monkey logo

keyvaluestorage's Introduction

KeyValueStorage - A key-value storage module for Swift projects

Build Status CocoaPods Version License Platform

This module is an ultra-light wrapper for key-value storage frameworks like:

  • Keychain
  • User Defaults
  • Memory (Dictionary)

It is very easy to use, like the Keychain, for instance:

import KeyValueStorage
let myKey = KeyValueStorageKey(type: .keychain, value: "myKey")
let storage: KeyValueStorageProtocol = KeyValueStorage()
storage.set(value: "something awesome!", for: myKey)
let value: String? = storage.get(key: myKey)
print(value)

Nice, right? What about the others? Well, you can easily create a key for any of the storage types:

let keychainKey = KeyValueStorageKey(type: .keychain, value: "myKey")
let defaultsKey = KeyValueStorageKey(type: .defaults, value: "myKey")
let memoryKey = KeyValueStorageKey(type: .memory, value: "myKey")

Why?

You might be asking why another framework for this kind of feature? It's because all the storage frameworks are together? No, not only for that. Actually, this framework has a bigger purpose, it is completely based on the KeyValueStorageProtocol which provides testability for your app. The way to do it is using the protocol on parameters and properties so you could inject dependencies for different behaviours. You can use the one in the package or implement a mock to avoid some issues on the tests, for instance.

Setup

CocoaPods

If you are using CocoaPods, add this to your Podfile and run pod install.

target 'Your target name' do
    pod 'KeyValueStorage', '~> 2.0'
end

Manual Installation

If you want to add it manually to your project, without a package manager, just copy all files from the Classes folder to your project.

Usage

Add import KeyValueStorage to your source code.

Store values

The KeyValueStorageProtocol supports any kind of Codable values, so you can use them as you want to store data into any of the storage types:

storage.set(value: 10, for: myKey)
let intValue: Int? = storage.get(key: myKey)

storage.set(value: "abc", for: myKey)
let stringValue: String? = storage.get(key: myKey)

Removing keys from the storage

storage.remove(key: myKey) // Removes a single key
storage.clean(type: .memory) // Removes all keys from the given storage type
storage.cleanAll() // Removes all keys from all storage types

KeyValueStorageKey

To create keys, we use this special object called KeyValueStorageKey. As you can see, it is very simple:

public struct KeyValueStorageKey: Equatable, Hashable {
    
    // MARK: - Properties
    
    public let type: KeyValueStorageType
    public let value: String
    
    // MARK: - Initialization
    
    public init(type: KeyValueStorageType, value: String) {
        self.type = type
        self.value = value
    }
}

You can extend it with your own keys to use the storage without hardcoded strings everywhere. You just need to add a static property in an extension of the KeyValueStorageKey and then use it with autocomplete:

extension KeyValueStorageKey {
	static let myKey = KeyValueStorageKey(type: .memory, value: "myKey")
}

storage.set(value: "abcde", for: .myKey)
let value: String? = storage.get(key: .myKey)

This is how we can safely reuse keys throughout the app and it is even better in local frameworks because you can create internal extensions for each one.

Thanks ๐Ÿ‘

The creation of this framework was possible thanks to these awesome people:

Feedback is welcome

If you notice any issue, got stuck or just want to chat feel free to create an issue. We will be happy to help you.

License

KeyValueStorage is released under the MIT License.

keyvaluestorage's People

Stargazers

 avatar  avatar  avatar  avatar  avatar

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.