{"id":25899391,"url":"https://github.com/zijievv/user-defaults-observation","last_synced_at":"2025-03-03T01:28:29.450Z","repository":{"id":193414432,"uuid":"688582406","full_name":"zijievv/user-defaults-observation","owner":"zijievv","description":"A Swift macro adding accessors to properties for reading/writing values in UserDefaults within Observable classes","archived":false,"fork":false,"pushed_at":"2024-06-30T07:48:50.000Z","size":33,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-07-01T16:22:03.781Z","etag":null,"topics":["macro","macros","observable","observation","swift","swift-macro","swift-macros","swift-package-manager","swiftui","userdefaults"],"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/zijievv.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-07T16:44:45.000Z","updated_at":"2024-06-30T07:48:54.000Z","dependencies_parsed_at":"2024-06-23T06:45:51.527Z","dependency_job_id":"c492b22b-a5e5-414a-90aa-5d600bff0bef","html_url":"https://github.com/zijievv/user-defaults-observation","commit_stats":null,"previous_names":["zijievv/user-defaults-observation"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zijievv%2Fuser-defaults-observation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zijievv%2Fuser-defaults-observation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zijievv%2Fuser-defaults-observation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zijievv%2Fuser-defaults-observation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zijievv","download_url":"https://codeload.github.com/zijievv/user-defaults-observation/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241594303,"owners_count":19987775,"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":["macro","macros","observable","observation","swift","swift-macro","swift-macros","swift-package-manager","swiftui","userdefaults"],"created_at":"2025-03-03T01:28:29.046Z","updated_at":"2025-03-03T01:28:29.443Z","avatar_url":"https://github.com/zijievv.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# UserDefaultsObservation\n\n`ObservableUserDefaults` is a Swift macro adding accessors to properties for reading/writing values in `UserDefaults` within `Observable` classes.\n\n## Usage\n\nSource code:\n\n```swift\nimport Foundation\nimport Observation\nimport UserDefaultsObservation\n\n@Observable\nclass Model {\n    @ObservableUserDefaults(key: \"text\", store: Self.store)\n    @ObservationIgnored\n    var text: String = \"Text\"\n\n    @ObservableUserDefaults(key: \"value\")\n    @ObservationIgnored\n    var value: Int = 1\n\n    static let store = UserDefaults(suiteName: \"Store\")!\n}\n```\n\nExpanded source:\n\n```swift\nimport Foundation\nimport Observation\nimport UserDefaultsObservation\n\n@Observable\nclass Model {\n    @ObservationIgnored\n    var text: String {\n        @storageRestrictions(initializes: _text)\n        init(initialValue) {\n            _text = initialValue\n        }\n        get {\n            access(keyPath: \\.text)\n            let store: UserDefaults = Self.store\n            return (store.value(forKey: \"text\") as? String) ?? _text\n        }\n        set {\n            withMutation(keyPath: \\.text) {\n                let store: UserDefaults = Self.store\n                store.set(newValue, forKey: \"text\")\n            }\n        }\n    }\n    \n    @ObservationIgnored private let _text: String\n\n    @ObservationIgnored\n    var value: Int = 1 {\n        @storageRestrictions(initializes: _value)\n        init(initialValue) {\n            _value = initialValue\n        }\n        get {\n            access(keyPath: \\.value)\n            let store: UserDefaults = .standard\n            return (store.value(forKey: \"value\") as? Int) ?? _value\n        }\n        set {\n            withMutation(keyPath: \\.value) {\n                let store: UserDefaults = .standard\n                store.set(newValue, forKey: \"value\")\n            }\n        }\n    }\n    \n    @ObservationIgnored private let _value: Int\n\n    static let store = UserDefaults(suiteName: \"Store\")!\n}\n```\n\n⚠️ Note that when you use the `@ObservableUserDefaults(key:store:)` macro, you need to add the `@ObservationIgnored` macro to the property. Otherwise, `@Observable` will generate accessors that conflict with `ObservableUserDefaults`.\n\n## Installation\n\n### [Swift Package Manager](https://www.swift.org/package-manager/) (SPM)\n\nAdd the following line to the dependencies in `Package.swift`, to use the `ObservableUserDefaults` macro in a SPM project:\n\n```swift\n.package(url: \"https://github.com/zijievv/user-defaults-observation\", from: \"0.1.0\"),\n```\n\nIn your target:\n\n```swift\n.target(name: \"\u003cTARGET_NAME\u003e\", dependencies: [\n    .product(name: \"UserDefaultsObservation\", package: \"user-defaults-observation\"),\n    // ...\n]),\n```\n\nAdd `import UserDefaultsObservation` into your source code to use the `ObservableUserDefaults` macro.\n\n### Xcode\n\nGo to `File \u003e Add Package Dependencies...` and paste the repo's URL:\n\n```\nhttps://github.com/zijievv/user-defaults-observation\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzijievv%2Fuser-defaults-observation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzijievv%2Fuser-defaults-observation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzijievv%2Fuser-defaults-observation/lists"}