{"id":999,"url":"https://github.com/IvanRublev/PersistentStorageSerializable","last_synced_at":"2025-07-30T20:30:44.725Z","repository":{"id":62450629,"uuid":"87628677","full_name":"IvanRublev/PersistentStorageSerializable","owner":"IvanRublev","description":"Swift library that makes easier to serialize the user's preferences (app's settings) with system User Defaults or Property List file on disk.","archived":false,"fork":false,"pushed_at":"2017-09-26T10:29:26.000Z","size":219,"stargazers_count":159,"open_issues_count":0,"forks_count":4,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-06-27T08:52:28.709Z","etag":null,"topics":["cocoapod","nsuserdefaults","plist","serialization","serialization-library","swift-library","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/IvanRublev.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-04-08T11:20:58.000Z","updated_at":"2025-05-16T08:10:55.000Z","dependencies_parsed_at":"2022-11-01T23:33:06.167Z","dependency_job_id":null,"html_url":"https://github.com/IvanRublev/PersistentStorageSerializable","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/IvanRublev/PersistentStorageSerializable","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IvanRublev%2FPersistentStorageSerializable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IvanRublev%2FPersistentStorageSerializable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IvanRublev%2FPersistentStorageSerializable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IvanRublev%2FPersistentStorageSerializable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/IvanRublev","download_url":"https://codeload.github.com/IvanRublev/PersistentStorageSerializable/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IvanRublev%2FPersistentStorageSerializable/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267813312,"owners_count":24148321,"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","status":"online","status_checked_at":"2025-07-30T02:00:09.044Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["cocoapod","nsuserdefaults","plist","serialization","serialization-library","swift-library","userdefaults"],"created_at":"2024-01-05T20:15:36.624Z","updated_at":"2025-07-30T20:30:44.397Z","avatar_url":"https://github.com/IvanRublev.png","language":"Swift","funding_links":[],"categories":["Database"],"sub_categories":["Getting Started","Other free courses","Linter"],"readme":"# PersistentStorageSerializable\n\n[![CI Status](http://img.shields.io/travis/IvanRublev/PersistentStorageSerializable.svg?style=flat)](https://travis-ci.org/IvanRublev/PersistentStorageSerializable)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n[![Version](https://img.shields.io/cocoapods/v/PersistentStorageSerializable.svg?style=flat)](http://cocoapods.org/pods/PersistentStorageSerializable)\n[![Swift](https://img.shields.io/badge/Swift-3.1-orange.svg?style=flat)](https://img.shields.io/badge/Swift-3.1-orange.svg?style=flat)\n[![License](https://img.shields.io/cocoapods/l/PersistentStorageSerializable.svg?style=flat)](http://cocoapods.org/pods/PersistentStorageSerializable)\n\n`PersistentStorageSerializable` is a protocol for automatic serialization and deserialization of Swift class, struct or NSObject descendant object into and from User Defaults or Property List file.\n\nThe adopting type properties must be of property list type (String, Data, Date, Int, UInt, Float, Double, Bool, Array or Dictionary of above). If you want to store any other type of object, you should typically archive it to create an instance of Data. The URL properties can be stored in User Defaults storage but not in Plist storage. In the last case, you have to archive it to/from Data.\n\nThe `PersistentStorageSerializable` protocol provides default implementations of `init(from:)` initializer and `persist()` function. The library defines two classes of `PesistentStorage` protocol: `UserDefaultsStorage` and `PlistStorage`. Object of one of those types is passed as the argument when calling to `init(from:)` initializer to specify which storage to be used for serialization/deserialization.\n\nFunctions of the `PersistentStorageSerializable` protocol traverses the adopting type object and gets/sets it's properties values via [Reflection](https://github.com/Zewo/Reflection) library. The NSObject class descendant properties values are get/set via KVC.\n\n## How to use\n\nSerialize/Deserialize a struct with User Defaults by using `UserDefaultStorage` as shown below:\n\n```swift\nstruct Settings: PersistentStorageSerializable {\n    var flag = false\n    var title = \"\"\n    var number = 1\n    var dictionary: [String : Any] = [\"Number\" : 1, \"Distance\" : 25.4, \"Label\" : \"Hello\"]\n\n    // MARK: Adopt PersistentStorageSerializable\n    var persistentStorage: PersistentStorage!\n    var persistentStorageKeyPrefix: String! = \"Settings\"\n}\n\n// Init from User Defaults\nvar mySettings = try! Settings(from: UserDefaultsStorage.standard)\n\nmySettings.flag = true\n\n// Persist into User Defaults\ntry! mySettings.persist()\n```\n\nTo serialize data with Plist file use `PlistStorage` class:\n\n```swift\n// Init from plist\nlet plistUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last!.appendingPathComponent(\"storage.plist\")\n\nvar settingsOnDisk = try! Settings(from: PlistStorage(at: plistUrl))\n\nmySettings.flag = true\n\n// Persist on disk\ntry! mySettings.persist()\n```\n\n### Reading data stored by the previous version of the app\n\nWhen you have some data persisted in User Defaults by the previous version of the app and want to read that data into a structure you need to provide a mapping between properties names and User Defaults keys by overloading the `persistentStorageKey(for:)` function.\n\nSay we have following data persisted in User Defaults:\n\n```swift\nUserDefaults.standard.set(\"Superhero\", forKey: \"oldGoogTitle\")\nUserDefaults.standard.set(true, forKey: \"well.persisted.option\")\n```\n\nWe want those to be serialized with the object of `ApplicationConfiguration` class.\n\n```swift\nfinal class ApplicationConfiguration: PersistentStorageSerializable {\n    var title = \"\"\n    var showIntro = false\n\n    // MARK: Adopt PersistentStorageSerializable\n    var persistentStorage: PersistentStorage!\n    var persistentStorageKeyPrefix: String!\n}\n\n// Provide key mapping by overloading `persistentStorageKey(for:)` function.\nextension ApplicationConfiguration {\n    func persistentStorageKey(for propertyName: String) -\u003e String {\n        let keyMap = [\"title\" : \"oldGoogTitle\", \"showIntro\" : \"well.persisted.option\"]\n        return keyMap[propertyName]!\n    }\n}\n\n// Now we can load data persisted in the storage.\nlet configuration = try! ApplicationConfiguration(from: UserDefaultsStorage.standard)\n\nprint(configuration.title) // prints Superhero\nprint(configuration.showIntro) // prints true\n```\n\n## Example\n\nTo run example projects for iOS/macOS, run `pod try PersistentStorageSerializable` in the terminal.\n\n## Requirements\n\n- Xcode 8.3\n- Swift 3.1\n\n## Installation\n\n### CocoaPods\n\nPersistentStorageSerializable is available through [CocoaPods](http://cocoapods.org). To install it, simply add the following line to your Podfile:\n\n```ruby\npod \"PersistentStorageSerializable\"\n```\n\nand run `pods update` or `pods install`.\n\n\u003e :sparkles: For Xcode 9, Swift 4 and cocoapods installation, please follow the [instruction here](https://github.com/IvanRublev/PersistentStorageSerializable/issues/2#issuecomment-332145439).\n\n### Carthage\n\nIf you use Carthage to manage your dependencies, simply add PersistentStorageSerializable to your Cartfile:\n\n```\ngithub \"IvanRublev/PersistentStorageSerializable\"\n```\n\nIf you use Carthage to build your dependencies, make sure you have added `PersistentStorageSerializable.framework` and `Reflection.framework` to the \"Linked Frameworks and Libraries\" section of your target, and have included them in your Carthage framework copying build phase.\n\n## Author\n\nCopyright (c) 2017, IvanRublev, ivan@ivanrublev.me\n\n## License\n\nPersistentStorageSerializable 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%2FIvanRublev%2FPersistentStorageSerializable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FIvanRublev%2FPersistentStorageSerializable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FIvanRublev%2FPersistentStorageSerializable/lists"}