{"id":28826429,"url":"https://github.com/danielctull/userdefaultskey","last_synced_at":"2026-04-28T17:04:57.011Z","repository":{"id":63907422,"uuid":"233311665","full_name":"danielctull/UserDefaultsKey","owner":"danielctull","description":"A swift package to specify a Key type for use with UserDefaults.","archived":false,"fork":false,"pushed_at":"2021-04-06T07:29:17.000Z","size":51,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-06-19T03:04:00.206Z","etag":null,"topics":["ios","macos","swift","swift-package"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/danielctull.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-01-11T23:30:35.000Z","updated_at":"2022-04-28T12:22:01.000Z","dependencies_parsed_at":"2022-11-28T20:16:19.255Z","dependency_job_id":null,"html_url":"https://github.com/danielctull/UserDefaultsKey","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/danielctull/UserDefaultsKey","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielctull%2FUserDefaultsKey","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielctull%2FUserDefaultsKey/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielctull%2FUserDefaultsKey/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielctull%2FUserDefaultsKey/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danielctull","download_url":"https://codeload.github.com/danielctull/UserDefaultsKey/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielctull%2FUserDefaultsKey/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260676922,"owners_count":23045113,"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":["ios","macos","swift","swift-package"],"created_at":"2025-06-19T03:03:59.182Z","updated_at":"2026-04-28T17:04:56.999Z","avatar_url":"https://github.com/danielctull.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# UserDefaultsKey\n\n[![Test results][tests shield]][actions] [![Latest release][release shield]][releases] [![Swift 5.1][swift shield]][swift] ![Platforms: iOS, macOS, tvOS, watchOS, Linux][platforms shield]\n\nA package to specify a Key type for use with UserDefaults. \n\n## Usage\n\nGenerally you will want to extend the UserDefaultsKey to add a static property to allow easy access to the key.\n\n```swift\nextension UserDefaultsKey where Value == Bool {\n    static let somePreference = UserDefaultsKey(\"somePreference\", default: false)\n}\n```\n\nThen you can access the value using the key with shorthand syntax. The value you get back will be the one specified at the declaration of the key. \n\n```swift\nlet somePreference = UserDefaults.standard.value(for: .somePreference)\nif somePreference {\n\n} else {\n\n}\n```\n\nBecause the key has the type of the value, you can only store a value of the correct type for that key.\n\n```swift\nUserDefaults.standard.set(true, for: .somePreference) // Works\n\nUserDefaults.standard.set(\"true\", for: .somePreference) // Fails to compile\n```\n\n## Optional Values\n\nKeys can be defined with optional values, and can either then fall back to a default value or nil. The initialiser that only takes the key name will return a key that falls back to nil. When declaring optional keys, you will also need to supply the type for Value as a generic parameter. \n\n```swift\nextension UserDefaultsKey where Value == String? {\n    static let optionalPreference = UserDefaultsKey\u003cString?\u003e(\"optionalPreference\")\n}\n```\n\n## Property Wrapper\n\nOnce you have defined a key, you can pass it to the UserDefault property wrapper to allow succinct definitions of properties that are backed by user defaults.\n\n```swift\nstruct Foo {\n    @UserDefault(.somePreference) var somePreference\n}\n```\n\nBecause Swift infers the value type from the key, you don't need to specify it in the property definition. For clarity however, it may be desirable to do so.\n\n```swift\nstruct Foo {\n    @UserDefault(.somePreference) var somePreference: Bool\n    \n    func bar() {\n        \n        if somePreference {\n        \n        } else {\n        \n        }\n    }\n}\n```\n\n[swift]: https://swift.org\n\n[releases]: https://github.com/danielctull/UserDefaultsKey/releases\n[release shield]: https://img.shields.io/github/v/release/danielctull/UserDefaultsKey\n[swift shield]: https://img.shields.io/badge/swift-5.1-F05138.svg \"Swift 5.1\"\n[platforms shield]: https://img.shields.io/badge/platforms-iOS_macOS_tvOS_watchOS_Linux-lightgrey.svg?style=flat \"iOS, macOS, tvOS, watchOS, Linux\"\n\n[actions]: https://github.com/danielctull/UserDefaultsKey/actions\n[tests shield]: https://github.com/danielctull/UserDefaultsKey/workflows/tests/badge.svg\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielctull%2Fuserdefaultskey","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanielctull%2Fuserdefaultskey","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielctull%2Fuserdefaultskey/lists"}