{"id":15066990,"url":"https://github.com/jrsaruo/safedefaultskit","last_synced_at":"2025-10-05T03:31:44.679Z","repository":{"id":62454846,"uuid":"114768238","full_name":"jrsaruo/SafeDefaultsKit","owner":"jrsaruo","description":"Pure-Swift library for safe access to UserDefaults.","archived":true,"fork":false,"pushed_at":"2022-06-15T05:18:48.000Z","size":35,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-30T15:36:27.982Z","etag":null,"topics":["carthage","cocoapods","swift","swift4","userdefaults","wrapper","xcode"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jrsaruo.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":"2017-12-19T13:27:24.000Z","updated_at":"2023-01-27T23:53:51.000Z","dependencies_parsed_at":"2022-11-02T00:16:34.050Z","dependency_job_id":null,"html_url":"https://github.com/jrsaruo/SafeDefaultsKit","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrsaruo%2FSafeDefaultsKit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrsaruo%2FSafeDefaultsKit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrsaruo%2FSafeDefaultsKit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrsaruo%2FSafeDefaultsKit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jrsaruo","download_url":"https://codeload.github.com/jrsaruo/SafeDefaultsKit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235360933,"owners_count":18977603,"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":["carthage","cocoapods","swift","swift4","userdefaults","wrapper","xcode"],"created_at":"2024-09-25T01:14:50.048Z","updated_at":"2025-10-05T03:31:39.377Z","avatar_url":"https://github.com/jrsaruo.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SafeDefaultsKit\n\n:warning: **This repository is archived.** :warning:\n\n![iOS 10.0+](https://img.shields.io/badge/platform-iOS%2010%2B-blue.svg?style=flat)\n![Swift 4.0](https://img.shields.io/badge/Swift-4.0-orange.svg?style=flat)\n![Cocoapods Compatible](https://img.shields.io/cocoapods/v/SafeDefaultsKit.svg)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n\nPure-Swift library for safe access to UserDefaults\n- [Requirements](#requirements)\n- [Installation](#installation)\n- [Features](#features)\n- [Usage](#usage)\n- [Caution](#caution)\n- [License](#license)\n\n\n## Requirements\n* Xcode 9\n* Swift 4.0+\n* iOS 10.0+\n\n\n## Installation\n### CocoaPods\n[CocoaPods](http://cocoapods.org) is a dependency manager for Cocoa projects.\n\nTo integrate SafeDefaultsKit into your Xcode project using CocoaPods, add the following line to your `Podfile` and run `pod install`.\n\n```ruby\npod 'SafeDefaultsKit', git: 'https://github.com/jrsaruo/SafeDefaultsKit.git'\n```\n\n### Carthage\n[Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.\n\nTo integrate SafeDefaultsKit into your Xcode project using Carthage, specify it in your `Cartfile`:\n\n```ogdl\ngithub \"jrsaruo/SafeDefaultsKit\"\n```\n\nRun `carthage update` to build the framework and drag the built `SafeDefaultsKit.framework` into your Xcode project.\n\n\n## Features\n### Safe access by using Enum as key\nYou can define original keys and safely access to `UserDefaults` by using them.\n```swift\n// Initialize your Defaults\nlet defaults = YourDefaults()\n\n// Set value for Defaults\ndefaults.set(\"saved string\", forKey: .definedKey)\n\n// Get value from Defaults\nprint(defaults.string(forKey: .definedKey) ?? \"no value\") // -\u003e \"saved string\"\n\n// Remove value from Defaults\ndefaults.removeObject(forKey: .definedKey)\nprint(defaults.string(forKey: .definedKey) ?? \"no value\") // -\u003e \"no value\"\n```\n\n### Divide Defaults\nBy dividing Defaults for each purposes, it becomes easier to manage keys.\n```swift\nlet loginInfoDefaults = LoginInfoDefaults() // LoginInfoDefaults has \"loginDate\" key\nloginInfoDefaults.set(Date(), forKey: .loginDate)\n\nlet settingDefaults = SettingDefaults() // SettingDefaults has \"mainColor\" key\nsettingDefaults.set(\"red\", forKey: .mainColor)\n```\n\n\n## Usage\nAt first, declare the type that conforms to the `SafeDefaults` protocol. This requires nested Enum named `Keys` that conforms to the `DefaultsKey` protocol and stores **string raw values**, and its cases are used as keys for your Defaults.\n\n### Simplest Declaration\n```swift\nstruct SampleDefaults: SafeDefaults {\n\n    enum Keys: String, DefaultsKey {\n        case userName\n    }\n\n}\n\nSampleDefaults().set(\"Tom\", forKey: .userName)\n// same as: UserDefaults.standard.set(\"Tom\", forKey: \"userName\")\n```\n\n### Customize the key name for UserDefaults\n`DefaultsKey` requires the `uniqueValue` property, used as the key name for `UserDefaults`.\n`uniqueValue`'s **default value is its raw value**.\n```swift\nstruct SampleDefaults: SafeDefaults {\n\n    enum Keys: String, DefaultsKey {\n        case userName = \"customizedKeyForUserName\"\n    }\n}\n\nSampleDefaults().set(\"Tom\", forKey: .userName)\n// same as: UserDefaults.standard.set(\"Tom\", forKey: \"customizedKeyForUserName\")\n```\n\nIf you declare `uniqueValue` explicitly, it is used as the key name instead of the raw value.\n```swift\nstruct SampleDefaults: SafeDefaults {\n\n    enum Keys: String, DefaultsKey {\n        case userName\n\n        // customize the key name for UserDefaults\n        var uniqueValue: String {\n            return \"SampleDefaults.\\(Keys.self).\\(self.rawValue)\"\n        }\n    }\n\n}\n\nSampleDefaults().set(\"Tom\", forKey: .userName)\n// same as: UserDefaults.standard.set(\"Tom\", forKey: \"SampleDefaults.Keys.userName\")\n```\n\n\n\u003ca name=\"caution\"\u003e\u003c/a\u003e\n## :warning: Caution\n### Difference from UserDefaults\ngetter methods for double, float, integer return **optional** value. If the specified key doesn‘t exist, these methods return `nil`.\n```swift\nUserDefaults.standard.removeObject(forKey: \"removed\")\n\nUserDefaults.standard.integer(forKey: \"removed\") // -\u003e 0\nYourDefaults().integer(forKey: .removed) // -\u003e nil\n```\n\n### Keys.uniqueValue is NOT guaranteed to be unique entirely\n`uniqueValue` is used as key name for `UserDefaults` and default is its raw value, so **there is fear that some keys conflict each other**.\n\n#### :x: NG sample\n```swift\nstruct BookDefaults: SafeDefaults {\n    enum Keys: String, DefaultsKey {\n        case id\n    }\n}\n\nstruct ComicDefaults: SafeDefaults {\n    enum Keys: String, DefaultsKey {\n        case id // MARK: This uniqueValue conflicts with that of BookDefaults.Keys.id\n    }\n}\n\nBookDefaults().set(5, forKey: .id) // same as: UserDefaults.set(5, forKey: \"id\")\nComicDefaults().set(10, forKey: .id) // same as: UserDefaults.set(10, forKey: \"id\")\nprint(BookDefaults().integer(forKey: .id)!) // -\u003e 10 (not 5)\n```\n\n#### :o: Avoid the same case name\n```swift\nstruct BookDefaults: SafeDefaults {\n    enum Keys: String, DefaultsKey {\n        case bookID\n    }\n}\n\nstruct ComicDefaults: SafeDefaults {\n    enum Keys: String, DefaultsKey {\n        case comicID\n    }\n}\n\nBookDefaults().set(5, forKey: .bookID) // same as: UserDefaults.set(5, forKey: \"bookID\")\nComicDefaults().set(10, forKey: .comicID) // same as: UserDefaults.set(10, forKey: \"comicID\")\nprint(BookDefaults().integer(forKey: .bookID)!) // -\u003e 5\n```\n\n#### :o: Set differerent rawValues explicitly\n```swift\nstruct BookDefaults: SafeDefaults {\n    enum Keys: String, DefaultsKey {\n        case id = \"bookID\"\n    }\n}\n\nstruct ComicDefaults: SafeDefaults {\n    enum Keys: String, DefaultsKey {\n        case id = \"comicID\"\n    }\n}\n\nBookDefaults().set(5, forKey: .id) // same as: UserDefaults.set(5, forKey: \"bookID\")\nComicDefaults().set(10, forKey: .id) // same as: UserDefaults.set(10, forKey: \"comicID\")\nprint(BookDefaults().integer(forKey: .id)!) // -\u003e 5\n```\n\n#### :o: Declare uniqueValue by yourself\nIt is possible to customize key name for UserDefaults by declaring `uniqueValue`. For example, you can use your Defaults' name as the namespace.\n```swift\nstruct BookDefaults: SafeDefaults {\n    enum Keys: String, DefaultsKey {\n        case id\n\n        var uniqueValue: String {\n            // use \"BookDefaults.Keys\" as the namespace\n            return \"BookDefaults.\\(Keys.self).\\(self.rawValue)\"\n        }\n    }\n}\n\nstruct ComicDefaults: SafeDefaults {\n    enum Keys: String, DefaultsKey {\n        case id\n\n        var uniqueValue: String {\n            return \"ComicDefaults.\\(Keys.self).\\(self.rawValue)\"\n        }\n    }\n}\n\nBookDefaults().set(5, forKey: .id) // same as: UserDefaults.set(5, forKey: \"BookDefaults.Keys.id\")\nComicDefaults().set(10, forKey: .id) // same as: UserDefaults.set(10, forKey: \"ComicDefaults.Keys.id\")\nprint(BookDefaults().integer(forKey: .id)!) // -\u003e 5\n```\n\n\n## License\nSafeDefaultsKit is under MIT license. See the [LICENSE](LICENSE) file for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjrsaruo%2Fsafedefaultskit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjrsaruo%2Fsafedefaultskit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjrsaruo%2Fsafedefaultskit/lists"}