Store values using unique, randomly generated identifiers

Overview

Storage

Store values using unique, randomly generated identifiers.

This packages consists of three types: A Storage class, a UniqueIdentifiable protocol, and a UniqueID struct.

You use Storage's auto-getting and setting methods to manipulate the values of computed properties without the need for extra, often unnecessary code.

Create a type, and declare conformance to the UniqueIdentifiable protocol. The only requirement is that you implement an id property of the UniqueID type.

struct MyType: UniqueIdentifiable {
    let id = UniqueID()
}

On its own, this has very few practical applications, but when combined with the Storage type, the list of applications grows. Storage's usefulness becomes apparent when dealing with protocol extensions. You can use the id property to get and set computed properties, something that otherwise would be very difficult to achieve, as extensions in Swift cannot contain stored properties.

fileprivate var storage = Storage()

protocol Rock: UniqueIdentifiable { }

extension Rock {
    var type: String? {
        get { storage.autoGet(id) }
        set { storage.autoSet(newValue, id) }
    }
    
    var color: String? {
        get { storage.autoGet(id) }
        set { storage.autoSet(newValue, id) }
    }
    
    var texture: String? {
        get { storage.autoGet(id) }
        set { storage.autoSet(newValue, id) }
    }
}

The properties above automatically get stored under new ids that are created by combining the id of the enclosing Rock instance with an id that is synthesized from the name of the property.

You can also provide a backup value in the event that the property has not been set. This lets you avoid having to mark the properties as optional, and provide a default state for every instance of a type. Let's change the property declarations we defined above and give them default values.

var type: String {
    get { storage.autoGet(id, backup: "pumice") }
    set { storage.autoSet(newValue, id) }
}

var color: String {
    get { storage.autoGet(id, backup: "gray") }
    set { storage.autoSet(newValue, id) }
}

var texture: String {
    get { storage.autoGet(id, backup: "rough") }
    set { storage.autoSet(newValue, id) }
}

You can also store static properties, using your type's typeID static identifier. Let's extend Rock one more time and add some characteristics that every rock has.

// Note: You probably wouldn't want to make these specific properties settable, 
// as it's very difficult to change a rock, but I'm running out of ideas.

extension Rock {
    static var biggerThanABreadbox: Bool {
        get { storage.autoGet(typeID, backup: .random()) }
        set { storage.autoSet(newValue, typeID) }
    }
    
    static var animalVegetableOrMineral: String {
        get { storage.autoGet(typeID, backup: "mineral") }
        set { storage.autoSet(newValue, typeID) }
    }
}
You might also like...
A simple Pokedex app written in Swift that implements the PokeAPI, using Combine and data driven UI.
A simple Pokedex app written in Swift that implements the PokeAPI, using Combine and data driven UI.

SwiftPokedex SwiftPokedex is a simple Pokedex app written by Viktor Gidlöf in Swift that implements the PokeAPI. For full documentation and implementa

ResponderChain is a library that passes events using the responder chain.

ResponderChain ResponderChain is a library that passes events using the responder chain.

Swift code to programmatically execute local or hosted JXA payloads without using the on-disk osascript binary

Swift code to programmatically execute local or hosted JXA payloads without using the on-disk osascript binary. This is helpful when you have Terminal access to a macOS host and want to launch a JXA .js payload without using on-disk osascript commands.

Merges a given number of PDF files into one file using the PDFKit framework

Titanium iOS PDF Merge Merges a given number of PDF files into one file using the PDFKit framework Requirements iOS 11+ Titanium SDK 9+ API's Methods

AnimeListSwiftUI - Anime quote list built with MVVM Swift 5 using Async/Await

How To In SwiftUI Async/Await AnimeListSwiftUI - Anime quote list built with MVVM Swift 5 using Async/Await Clones Clubhouse - App clone built with Sw

This is a app developed in Swift, using Object Oriented Programing, UIKit user interface programmatically, API Request and Kingfisher to load remote images

iOS NOW ⭐ This is a app developed in Swift, using Object Oriented Programing, UIKit user interface programmatically, API Request and Kingfisher to loa

Using UI Table View With Swift
Using UI Table View With Swift

News-App - Apple 관련 기사를 보여주는 News app을 만들 것입니다. - 자세한 과정은 리드미에 있습니다. Table View와 Table view controller Table View : Table의 크기를 지정할 수 있다. Table View Co

Parsing indeterminate types with Decodable and Either enum using Swift

Decodable + Either Parsing indeterminate types with Decodable and Either enum us

Verify New Zealand COVID Passes in iOS apps using Swift

NzCovidPass-Swift Swift library for verification of the NZ Covid Vaccination Pas

Releases(v0.1.0)
Owner
Jordan Baird
Jordan Baird
Swift Xid - Xid uses MongoDB Object ID algorighm1 to generate globally unique ids with base32 serialzation to produce shorter strings

Swift Xid - Xid uses MongoDB Object ID algorighm1 to generate globally unique ids with base32 serialzation to produce shorter strings

Uditha Atukorala 0 Jun 13, 2022
A macOS application displaying the thermal, voltage and current sensor values.

Sensors About A macOS application displaying the thermal, voltage and current sensor values. License Project is released under the terms of the MIT Li

Jean-David Gadina 82 Jan 3, 2023
A Codable Undefinable type for handling JSON undefined values.

Undefinable Overview The purpose of this type is represent the JSON undefined state in Swift structs and classes. The Undefinable enum is a generic wi

David Monagle 3 Dec 18, 2022
App Store Receipt Validation

Kvitto "Kvitto, it means Receipt in Swedish. The trend of using Swedish words for libraries is pretty big" -- Hugo Tunius via Twitter Allows parsing a

Cocoanetics 288 Sep 12, 2022
swift-highlight a pure-Swift data structure library designed for server applications that need to store a lot of styled text

swift-highlight is a pure-Swift data structure library designed for server applications that need to store a lot of styled text. The Highlight module is memory-efficient and uses slab allocations and small-string optimizations to pack large amounts of styled text into a small amount of memory, while still supporting efficient traversal through the Sequence protocol.

kelvin 4 Aug 14, 2022
StoredIn is a simple property wrapper library to store any value in anywhere

StoredIn StoredIn is a simple property wrapper library to store any value in anywhere. Installation Please use the Swift Package Manager. dependencies

Henrique Sasaki Yuya 2 Jul 4, 2022
Project shows how to unit test asynchronous API calls in Swift using Mocking without using any 3rd party software

UnitTestingNetworkCalls-Swift Project shows how to unit test asynchronous API ca

Gary M 0 May 6, 2022
Differific is a diffing tool that helps you compare Hashable objects using the Paul Heckel's diffing algorithm

Differific is a diffing tool that helps you compare Hashable objects using the Paul Heckel's diffing algorithm. Creating a chan

Christoffer Winterkvist 127 Jun 3, 2022
Plugin and runtime library for using protobuf with Swift

Swift Protobuf Welcome to Swift Protobuf! Apple's Swift programming language is a perfect complement to Google's Protocol Buffer ("protobuf") serializ

Apple 4.1k Dec 28, 2022
A Swift package for rapid development using a collection of micro utility extensions for Standard Library, Foundation, and other native frameworks.

ZamzamKit ZamzamKit is a Swift package for rapid development using a collection of micro utility extensions for Standard Library, Foundation, and othe

Zamzam Inc. 261 Dec 15, 2022