Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/moriturus/StoredIn
A Property Wrapper to store a value anywhere.
https://github.com/moriturus/StoredIn
ios ipados macos swift tvos watchos
Last synced: 22 days ago
JSON representation
A Property Wrapper to store a value anywhere.
- Host: GitHub
- URL: https://github.com/moriturus/StoredIn
- Owner: moriturus
- License: mit
- Created: 2022-05-30T17:06:33.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-06-27T17:54:23.000Z (over 2 years ago)
- Last Synced: 2024-05-02T02:56:28.075Z (7 months ago)
- Topics: ios, ipados, macos, swift, tvos, watchos
- Language: Swift
- Homepage:
- Size: 8.79 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# StoredIn
[![Swift Package Manager compatible](https://img.shields.io/badge/Swift%20Package%20Manager-compatible-brightgreen.svg)](https://github.com/apple/swift-package-manager)
`StoredIn` is a simple property wrapper library to store any value in anywhere.
## Installation
Please use the `Swift Package Manager`.
```swift
dependencies: [
.package(url: "https://github.com/moriturus/StoredIn.git", .upToNextMajor(from: "1.0.0"))
]
```## Usage
```swift
import StoredIn// implement the `Store` where your data are saved.
final class SharedDictionary: Store {
typealias Key = String
var inner: [String: Any] = [:]
func containsValue(forKey key: String) -> Bool {
self.inner[key] != nil
}
func value(forKey key: String) -> S? where S : Storable {
self.inner[key] as? S
}
func set(value: S, forKey key: String) where S : Storable {
self.inner[key] = value
}
}let sharedDictionary = SharedDictionary()
struct Foo {
@StoredIn(store: sharedDictionary, key: "key", default: 0, strategy: .default)
var value: Int
}var foo = Foo()
let x = foo.value // x == 0
foo.value = 1
sharedDictionary.inner["key"] as? Int == foo.value // true
```## Strategy
- `default`
- The default strategy. You can change stored value as many times as you need.
- `once`
- You can store a value only once.## License
This software is released under the MIT License.
See LICENSE file for details.