{"id":24758531,"url":"https://github.com/fweugene/swiftkeyvaluestore","last_synced_at":"2025-08-09T09:08:47.968Z","repository":{"id":62456563,"uuid":"142307748","full_name":"FWEugene/SwiftKeyValueStore","owner":"FWEugene","description":null,"archived":false,"fork":false,"pushed_at":"2018-08-06T10:57:56.000Z","size":374,"stargazers_count":2,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-05T15:34:02.988Z","etag":null,"topics":["keychain","swift4","type-safe","userdefaults"],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/FWEugene.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-07-25T13:59:13.000Z","updated_at":"2019-08-08T12:24:08.000Z","dependencies_parsed_at":"2022-11-02T00:00:40.139Z","dependency_job_id":null,"html_url":"https://github.com/FWEugene/SwiftKeyValueStore","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/FWEugene/SwiftKeyValueStore","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FWEugene%2FSwiftKeyValueStore","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FWEugene%2FSwiftKeyValueStore/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FWEugene%2FSwiftKeyValueStore/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FWEugene%2FSwiftKeyValueStore/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FWEugene","download_url":"https://codeload.github.com/FWEugene/SwiftKeyValueStore/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FWEugene%2FSwiftKeyValueStore/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264067156,"owners_count":23552154,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["keychain","swift4","type-safe","userdefaults"],"created_at":"2025-01-28T16:20:57.992Z","updated_at":"2025-07-07T11:12:27.884Z","avatar_url":"https://github.com/FWEugene.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SwiftKeyValueStore\n\n![Swift version](https://img.shields.io/badge/swift-4.0-orange.svg)\n\n#### Type-Safe Swift API for Key-Value Store database. \n\n###### SwiftKeyValueStore is an extention for `UserDefaults` and `SwiftKeychainWrapper` to provide simple, type-safe, expressive Swifty API with the benefits of static typing. Chose what type of the database you want to use - unencrypted `UserDefaults` or encrypted storage in `KeyChain`. Define your keys in one place, use value types easily, and get extra safety and convenient compile-time checks for free.\n\n\n# Version 0.1\n### Features\n\n**There's only three steps to using SwiftKeyValueStore:**\n\nStep 1: Chose Storage type `UserDefaults`. Use `standard` shared instance or create new instance. \n\n```swift\nvar DefaultsKeyValueStore = UserDefaults.standard\n```\n\nOr encrypted storage in `KeyChain`. Use `standard` shared instance or create new instance. \n```swift\nvar KeychainKeyValueStore = KeychainWrapper.standard\n```\n\nStep 2: Define your keys.  \n\n```swift\nextension KeyValueStoreKeys {\n    static let userName = KeyValueStoreKey\u003cString\u003e(\"UserNameKey\")\n    static let onboardingIsEnabled = KeyValueStoreKey\u003cBool\u003e(\"OnboardingIsEnabledKey\")\n}\n```\n\nStep 3: Use it\n\n```swift\n//Set and get Keychain.\nKeychainKeyValueStore[.userName] = \"user@name.com\"\nlet username = KeychainKeyValueStore[.userName]\n\n//Set and get User defaults \nDefaultsKeyValueStore[.onboardingIsEnabled] = true\n\n// Modify value types in place\nDefaultsKeyValueStore[.launchCount] += 1\n\n// Use and modify typed arrays\nDefaultsKeyValueStore[.movies].append(\"StarWars\")\nDefaultsKeyValueStore[.movies][0] += \" Last Jedi\"\n\n// Works with types that conform Codable or NSCoding\nDefaultsKeyValueStore[.color] = UIColor.white\nDefaultsKeyValueStore[.color]?.whiteComponent // =\u003e 1.0\n```\n\nThe convenient dot syntax is only available if you define your keys by extending `KeyValueStoreKeys` class. Or just pass the `KeyValueStoreKey` value in square brackets. Or use String to create key with specified `ValutType` or default Value. \n\n## Usage\n\n### Define your keys\n\nDefine your user keys for your own convinience:\n\n```swift\nlet userKey = KeyValueStoreKey\u003cUser\u003e(\"userKey\")\nlet colorKey = \"ColorKey\".toKeyWith(type: UIColor)\nlet profilesKey = \"ProfilesKey\".toKeyWith(defaultValue: Array\u003cProfile\u003e())\n```\n\nCreate a `KeyValueStoreKey` object, provide the type of the value you want to store and the key name in parentheses.\nOr use `String` extension for your convinience to create `KeyValueStoreKey` from `String`\n\n\nCreate Instance of your store. You can use `UserDefault`s store or `KeyChainWrapper` store.\n\n```swift \nvar KeychainKeyValueStore = KeychainWrapper.standard\nvar DefaultsKeyValueStore = UserDefaults.standard\n```\nNow use the your store to access those values:\n\n```swift\n// store in UserDefaults\nDefaultsKeyValueStore[colorKey] = \"red\"\nDefaultsKeyValueStore[colorKey] // =\u003e UIColor.red, typed as UIColor?\n\n// store securely in KeyChain\nKeychainKeyValueStore[userKey] = User(firstName: \"Yuriy\", \n                                      lastName: \"Gagarin\") // struct User has to conform `Codable` protocol \n                                      \nKeychainKeyValueStore[userKey] // =\u003e (firstName: \"Yuriy\", \n                               //     lastName: \"Gagarin\"), typed as User?\n                                      \n```\n\nThe compiler would not let you to set a wrong value type, and alwasy returns expected optional type.\n\n\n### Supported types\n\nSwiftKeyValueStore supports all of the standard `NSUserDefaults` types, like strings, numbers, booleans, arrays and dictionaries. As well as any types the conforms Codable or NSCoding protocol\n\n#### Codable\n\n`SwiftKeyValueStore` support `Codable`. Just add `Codable` protcol conformance to your type, like:\n```swift\nstruct User: Codable {\n    let firstName: String\n    let lastName: String\n}\n```\n\nYou've got Array support for free:\n```swift\nlet users = KeyValueStoreKey\u003c[User]\u003e(\"users\")\n```\n\n#### NSCoding\n\n`SwiftKeyValueStore` support `NSCoding`. Just add `NSCoding` protcol conformance to your type and implement required methods:\n```swift\nclass UserProfileView: UIView, NSCoding  {\n    let userID: String\n\n    init(frame: CGRect, id: String) {\n        self.userID = id\n        super.init(frame: frame)\n    }\n\n    override func encode(with aCoder: NSCoder) {\n        aCoder.encode(userID, forKey: \"UserProfileView.Id\")\n        super.encode(with: aCoder)\n    }\n\n    required init?(coder aDecoder: NSCoder) {\n        guard let id = aDecoder.decodeObject(forKey: \"UserProfileView.Id\") as? String else { return nil }\n        self.userID = id\n        super.init(coder: aDecoder)\n    }\n}\n```\n\n#### Default values\n\n```swift\nlet counter = KeyValueStoreKey\u003cInt\u003e(\"counterKey\", defaultValue: 0)\nlet user = KeyValueStoreKey\u003cUser\u003e(\"token\", defaultValue: User(firstName: \"Anakin\", \n                                                              lastName: \"Skywalker\"))\n```\n\n\n### Remove all keys\n\nTo reset user defaults, use `resetStorage` method.\n\n```swift\nDefaultsKeyValueStore.resetStorage()\n```\n\n### Shared user defaults\n\nIf you're sharing your user defaults between different apps or an app and its extensions, you can create you onw instance of UserDefaults or KeyChainWrapper.\n\n```swift\nvar CustomSharedDefaults = UserDefaults(suiteName: \"my.amazing.app\")!\n```\n\n## Installation\n\n#### CocoaPods\n\nIf you're using CocoaPods, just add this line to your Podfile:\n\n```ruby\npod 'SwiftKeyValueStore'\n```\n\nInstall by running this command in your terminal:\n\n```sh\npod install\n```\nThen import the library in all files where you use it:\n\n```swift\nimport SwiftKeyValueStore\n```\nSwiftKeyValueStore is available under the MIT license. See the LICENSE file for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffweugene%2Fswiftkeyvaluestore","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffweugene%2Fswiftkeyvaluestore","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffweugene%2Fswiftkeyvaluestore/lists"}