{"id":23380520,"url":"https://github.com/xueqooy/association","last_synced_at":"2025-10-29T07:30:28.831Z","repository":{"id":269008141,"uuid":"906142239","full_name":"xueqooy/Association","owner":"xueqooy","description":"A swift library designed to dynamically associate values with objects at runtime","archived":false,"fork":false,"pushed_at":"2025-01-11T13:25:47.000Z","size":56,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-11T14:31:10.488Z","etag":null,"topics":["association","cocoa","objc-association","objc-runtime","swift"],"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/xueqooy.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":"2024-12-20T08:54:53.000Z","updated_at":"2025-01-11T13:25:50.000Z","dependencies_parsed_at":null,"dependency_job_id":"a35a5de0-5160-4fb6-9285-a4f9494ae17a","html_url":"https://github.com/xueqooy/Association","commit_stats":null,"previous_names":["xueqooy/association"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xueqooy%2FAssociation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xueqooy%2FAssociation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xueqooy%2FAssociation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xueqooy%2FAssociation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xueqooy","download_url":"https://codeload.github.com/xueqooy/Association/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238785243,"owners_count":19529929,"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":["association","cocoa","objc-association","objc-runtime","swift"],"created_at":"2024-12-21T20:16:36.416Z","updated_at":"2025-10-29T07:30:28.507Z","avatar_url":"https://github.com/xueqooy.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Association\n\n`Association` is a Swift library designed to dynamically associate values with objects at runtime.\n\n## At a Glance\n\nExtend UIView with an object.\n```swift\nclass CustomObject {}\n```\n\n```swift\nimport Association\n\nprivate let objectAssociation = Association\u003cCustomObject\u003e()\n\nextension UIView {\n    var customObject: CustomObject? {\n        get { Associations.objectAssociation[self] }\n        set { Associations.objectAssociation[self] = newValue }\n    }\n}\n```\n\ncomparison with Traditional Method:\n\n```swift\nimport ObjectiveC.runtime\n\nprivate var customObjectKey = \"customObjectKey\"\n\nextension UIView {\n    var customObject: CustomObject? {\n        get {\n            objc_getAssociatedObject(\n                self,\n                \u0026customObjectKey\n            ) as? CustomObject\n        }\n        set {\n            objc_setAssociatedObject(\n                self,\n                \u0026customObjectKey,\n                newValue,\n               .OBJC_ASSOCIATION_RETAIN_NONATOMIC\n            )\n        }\n    }\n}\n```\n\n### Tips and Tricks\n\n```swift\nstruct CustomStruct {}\nclass CustomObject {}\ntypealias Block = () -\u003e Void\n\n// By default, no wrapping is used, which corresponds to `none`.\nprivate let objectAssociation = Association\u003cCustomObject\u003e()\n\n// Use `weak` wrapping for weakly referenced associative objects.\nprivate let weakObjectAssociation = Association\u003cCustomObject\u003e(wrap: .weak)\n\n// It is recommended to use `direct` wrapping for custom value types.\n// For types that can be bridged to Objective-C (e.g., String, Bool, Int), wrapping may not be necessary.\n// Since Swift 3, custom value types are converted to `SwiftValue` in Objective-C, so wrapping may not be required.\nprivate let structAssociation = Association\u003cCustomStruct\u003e(wrap: .direct)\n\n// Closures should be associated using `direct` wrapping.\nprivate let blockAssociation = Association\u003cBlock\u003e(wrap: .direct)\n\nextension UIView {\n    var customStruct: CustomStruct? {\n        get { structAssociation[self] }\n        set { structAssociation[self] = newValue }\n    }\n\n    var customObject: CustomObject? {\n        get { objectAssociation[self] }\n        set { objectAssociation[self] = newValue }\n    }\n\n    var weakCustomObject: CustomObject? {\n        get { weakObjectAssociation[self] }\n        set { weakObjectAssociation[self] = newValue }\n    }\n\n    var block: Block? {\n        get { blockAssociation[self] }\n        set { blockAssociation[self] = newValue }\n    }\n}\n```\n\n## Installation\n\n**Using [Swift Package Manager](https://swift.org/package-manager)**:\n\n```swift\nimport PackageDescription\n\nlet package = Package(\n  name: \"MyAwesomeApp\",\n  dependencies: [\n    .Package(url: \"https://github.com/xueqooy/Association\", majorVersion: 1),\n  ]\n)\n```\n\n## License\n\n**Association** is under MIT license. See the [LICENSE](LICENSE) file for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxueqooy%2Fassociation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxueqooy%2Fassociation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxueqooy%2Fassociation/lists"}