{"id":1009,"url":"https://github.com/omaralbeik/UserDefaultsStore","last_synced_at":"2025-07-30T20:30:55.206Z","repository":{"id":32686262,"uuid":"139259807","full_name":"omaralbeik/UserDefaultsStore","owner":"omaralbeik","description":"Why not use UserDefaults to store Codable objects 😉","archived":true,"fork":false,"pushed_at":"2022-08-25T20:16:33.000Z","size":990,"stargazers_count":449,"open_issues_count":0,"forks_count":17,"subscribers_count":5,"default_branch":"main","last_synced_at":"2024-11-30T16:40:01.576Z","etag":null,"topics":["codable","database","ios","macos","swift","tvos","userdefaults","watchos"],"latest_commit_sha":null,"homepage":"https://omaralbeik.github.io/UserDefaultsStore/documentation/userdefaultsstore/","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/omaralbeik.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-06-30T15:29:31.000Z","updated_at":"2024-04-29T19:40:23.000Z","dependencies_parsed_at":"2022-09-16T15:10:50.732Z","dependency_job_id":null,"html_url":"https://github.com/omaralbeik/UserDefaultsStore","commit_stats":null,"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omaralbeik%2FUserDefaultsStore","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omaralbeik%2FUserDefaultsStore/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omaralbeik%2FUserDefaultsStore/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omaralbeik%2FUserDefaultsStore/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/omaralbeik","download_url":"https://codeload.github.com/omaralbeik/UserDefaultsStore/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228179124,"owners_count":17881135,"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":["codable","database","ios","macos","swift","tvos","userdefaults","watchos"],"created_at":"2024-01-05T20:15:36.927Z","updated_at":"2024-12-04T19:32:39.820Z","avatar_url":"https://github.com/omaralbeik.png","language":"Swift","readme":"\u003ch1 align=\"center\"\u003e⚠️ Deprecated ⚠️\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003ePlease use the new \u003ca href=\"https://github.com/omaralbeik/Stores\" target=\"_blank\"\u003eStores\u003c/a\u003e library instead.\u003c/p\u003e\n\n---\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://cdn.rawgit.com/omaralbeik/UserDefaultsStore/main/Assets/readme-logo.svg\" title=\"UserDefaultsStore\"\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://github.com/omaralbeik/UserDefaultsStore/actions\"\u003e\u003cimg src=\"https://github.com/omaralbeik/UserDefaultsStore/workflows/UserDefaultsStore/badge.svg?branch=main\" alt=\"Build Status\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://codecov.io/gh/omaralbeik/UserDefaultsStore\"\u003e\u003cimg src=\"https://codecov.io/gh/omaralbeik/UserDefaultsStore/branch/main/graph/badge.svg\" alt=\"Test Coverage\" /\u003e\u003c/a\u003e\n  \u003ca href=\"https://swiftpackageindex.com/omaralbeik/UserDefaultsStore\"\u003e\u003cimg src=\"https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fomaralbeik%2FUserDefaultsStore%2Fbadge%3Ftype%3Dswift-versions\" alt=\"Swift versions\" /\u003e\u003c/a\u003e\n  \u003ca href=\"https://swiftpackageindex.com/omaralbeik/UserDefaultsStore\"\u003e\u003cimg src=\"https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fomaralbeik%2FUserDefaultsStore%2Fbadge%3Ftype%3Dplatforms\" alt=\"Platforms\" /\u003e\u003c/a\u003e\n  \u003ca href=\"https://github.com/omaralbeik/UserDefaultsStore/blob/main/LICENSE\"\u003e\u003cimg src=\"https://img.shields.io/badge/License-MIT-red.svg\" alt=\"MIT\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n# tl;dr\nYou love Swift's `Codable` protocol and use it everywhere, who doesn't! Here is an easy and very light way to store and retrieve -**reasonable amount 😅**- of `Codable` objects, in a couple lines of code!\n\n---\n\n## New in v3.0\n\n- Both `UserDefaultsStore` and `SingleUserDefaultsStore` are thread safe!\n- **BREAKING**: Use of custom encoder/decoder has been removed.\n- **BREAKING**: Snapshots have been removed.\n\n---\n\n## Installation\n\n### Swift Package Manager\n\n1. Add the following to your `Package.swift` file:\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/omaralbeik/UserDefaultsStore.git\", from: \"3.0.0\")\n]\n```\n\n2. Build your project:\n\n```sh\n$ swift build\n```\n\n### Manually\n\nAdd the [Sources](https://github.com/omaralbeik/UserDefaultsStore/tree/main/Sources/UserDefaultsStore) folder to your Xcode project.\n\n---\n\n## Usage\n\nLet's say you have 2 structs; `User` and `Laptop` defined as bellow:\n\n```swift\nstruct User: Codable {\n    var id: Int\n    var firstName: String\n    var lastName: String\n    var laptop: Laptop?\n}\n```\n\n```swift\nstruct Laptop: Codable {\n    var model: String\n    var name: String\n}\n```\n\nHere is how you store them in **UserDefaultsStore**:\n\n\n### 1. Conform to the `Identifiable` protocol and set the `id` property\n\nThe `Identifiable` protocol lets UserDefaultsStore knows what is the unique id for each object.\n\n```swift\nstruct User: Codable, Identifiable {\n    ...\n}\n```\n\n```swift\nstruct Laptop: Codable, Identifiable {\n    var id: String { model }\n    ...\n}\n```\n\n### 2. Create UserDefaults Stores\n\n```swift\nlet usersStore = UserDefaultsStore\u003cUser\u003e(uniqueIdentifier: \"users\")\nlet laptopsStore = UserDefaultsStore\u003cLaptop\u003e(uniqueIdentifier: \"laptops\")\n```\n\n### 3. Voilà, you're all set!\n\n```swift\nlet macbook = Laptop(model: \"A1278\", name: \"MacBook Pro\")\nlet john = User(id: 1, firstName: \"John\", lastName: \"Appleseed\", laptop: macbook)\n\n// Save an object to a store\ntry! usersStore.save(john)\n\n// Save an array of objects to a store\ntry! usersStore.save([jane, steve, jessica])\n\n// Get an object from store\nlet user = store.object(withId: 1)\nlet laptop = store.object(withId: \"A1278\")\n\n// Get all objects in a store\nlet laptops = laptopsStore.allObjects()\n\n// Check if store has an object\nprint(usersStore.hasObject(withId: 10)) // false\n\n// Iterate over all objects in a store\nlaptopsStore.forEach { laptop in\n    print(laptop.name)\n}\n\n// Delete an object from a store\nusersStore.delete(withId: 1)\n\n// Delete all objects in a store\nlaptops.deleteAll()\n\n// Know how many objects are stored in a store\nlet usersCount = usersStore.objectsCount\n```\n\n## Looking to store a single item only?\n\nUse [`SingleUserDefaultsStore`](https://github.com/omaralbeik/UserDefaultsStore/tree/main/Sources/UserDefaultsStore/SingleUserDefaultsStore.swift), it enables storing and retrieving a single value of `Int`, `Double`, `String`, or any `Codable` type.\n\n## Requirements\n\n- iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 6.0+\n- Swift 5.0+\n\n\n## Thanks\n\nSpecial thanks to:\n- [Paul Hudson](https://twitter.com/twostraws) for his [article](https://www.hackingwithswift.com/articles/57/how-swift-keypaths-let-us-write-more-natural-code) on how to use Swift keypaths to write more natural code.\n- [Batuhan Saka](https://github.com/strawb3rryx7) for helping with translation into Turkish\n\n## Credits\n\nIcon made by [freepik](https://www.flaticon.com/authors/freepik) from [flaticon.com](https://www.flaticon.com).\n\n\n## License\n\nUserDefaultsStore is released under the MIT license. See [LICENSE](https://github.com/omaralbeik/UserDefaultsStore/blob/main/LICENSE) for more information.\n","funding_links":[],"categories":["Database","Libs","HarmonyOS","Data Management [🔝](#readme)"],"sub_categories":["Getting Started","Data Management","Linter","Windows Manager"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomaralbeik%2FUserDefaultsStore","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fomaralbeik%2FUserDefaultsStore","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomaralbeik%2FUserDefaultsStore/lists"}