{"id":16322256,"url":"https://github.com/p-x9/associatedobject","last_synced_at":"2025-04-05T16:06:07.790Z","repository":{"id":174555996,"uuid":"652402845","full_name":"p-x9/AssociatedObject","owner":"p-x9","description":"🔗 Swift Macro for allowing variable declarations even in class extensions","archived":false,"fork":false,"pushed_at":"2024-11-28T09:31:07.000Z","size":301,"stargazers_count":134,"open_issues_count":1,"forks_count":4,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-29T15:08:20.302Z","etag":null,"topics":["macros","swiftmacros","swiftpackage","swiftpackagemanager"],"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/p-x9.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-06-12T02:12:30.000Z","updated_at":"2025-03-26T22:13:43.000Z","dependencies_parsed_at":"2023-07-10T19:16:05.108Z","dependency_job_id":"a55477bf-625c-4f17-8569-e1f98d52c66c","html_url":"https://github.com/p-x9/AssociatedObject","commit_stats":null,"previous_names":["p-x9/associatedobject"],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p-x9%2FAssociatedObject","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p-x9%2FAssociatedObject/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p-x9%2FAssociatedObject/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p-x9%2FAssociatedObject/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/p-x9","download_url":"https://codeload.github.com/p-x9/AssociatedObject/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247361621,"owners_count":20926643,"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":["macros","swiftmacros","swiftpackage","swiftpackagemanager"],"created_at":"2024-10-10T22:50:25.260Z","updated_at":"2025-04-05T16:06:07.775Z","avatar_url":"https://github.com/p-x9.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AssociatedObject\nSwift Macro for allowing variable declarations even in class extensions.\nIt is implemented by wrapping `objc_getAssociatedObject`/`objc_setAssociatedObject`.\n\n\u003c!-- # Badges --\u003e\n\n[![Github issues](https://img.shields.io/github/issues/p-x9/AssociatedObject)](https://github.com/p-x9/AssociatedObject/issues)\n[![Github forks](https://img.shields.io/github/forks/p-x9/AssociatedObject)](https://github.com/p-x9/AssociatedObject/network/members)\n[![Github stars](https://img.shields.io/github/stars/p-x9/AssociatedObject)](https://github.com/p-x9/AssociatedObject/stargazers)\n[![Github top language](https://img.shields.io/github/languages/top/p-x9/AssociatedObject)](https://github.com/p-x9/AssociatedObject/)\n\n## Installation\n\n#### SPM\n```swift\n.package(url: \"https://github.com/p-x9/AssociatedObject\", from: \"0.10.3\")\n```\n\n#### CocoaPods\nAdd below to your `Podfile`.\n```\npod 'AssociatedObject', git: 'https://github.com/p-x9/AssociatedObject', tag: '0.10.3'\n```\n\nAfter `pod install`, you can use this Macro in your project.\n\nAdditionally, if you encounter build error like `Expansion of macro 'AssociatedObject' did not produce a non-observing accessor`. You should check your project setting `Build Settings`-`OTHER_SWIFT_FLAGS`.\n\nThere should be additional flags like so.\n![Alt text](image.png)\n\nIf not, you can add these two lines by yourself.\n```\n-load-plugin-executable\n${PODS_ROOT}/AssociatedObject/Binary/AssociatedObjectPlugin#AssociatedObjectPlugin\n```\n\n## Usage\nFor example, you can add a new stored property to `UIViewController` by declaring the following\n```swift\nimport AssociatedObject\n\nextension UIViewController {\n    @AssociatedObject(.retain(nonatomic))\n    var text = \"text\"\n\n    /* OR */\n\n    @AssociatedObject(.OBJC_ASSOCIATION_RETAIN_NONATOMIC)\n    var text = \"text\"\n\n    static var customKey = \"\"\n    @AssociatedObject(.OBJC_ASSOCIATION_RETAIN_NONATOMIC, key: customKey)\n    var somevar = \"text\"\n}\n```\n\nDeclared properties can be used as follows\n```swift\nclass ViewController: UIViewController {\n    override func viewDidLoad() {\n        super.viewDidLoad()\n\n        print(text) // =\u003e \"text\"\n\n        text = \"hello\"\n        print(text) // =\u003e \"hello\"\n    }\n\n}\n```\n### willSet/didSet\nProperties defined using `@AssociatedObject` can implement willSet and didSet.\nIn swift, it is not possible to implement `willSet` and `didSet` at the same time as setter, so they are expanded as follows.\n\n```swift\n@AssociatedObject(.copy(nonatomic))\npublic var hello: String = \"こんにちは\" {\n    didSet {\n        print(\"didSet\")\n    }\n    willSet {\n        print(\"willSet: \\(newValue)\")\n    }\n}\n\n// ↓↓↓ expand to ... ↓↓↓\npublic var hello: String = \"こんにちは\" {\n    get {\n        objc_getAssociatedObject(\n            self,\n            \u0026Self.__associated_helloKey\n        ) as? String\n        ?? \"こんにちは\"\n    }\n\n    set {\n        let willSet: (String) -\u003e Void = { [self] newValue in\n            print(\"willSet: \\(newValue)\")\n        }\n        willSet(newValue)\n\n        let oldValue = hello\n\n        objc_setAssociatedObject(\n            self,\n            \u0026Self.__associated_helloKey,\n            newValue,\n            .copy(nonatomic)\n        )\n\n        let didSet: (String) -\u003e Void = { [self] oldValue in\n            print(\"didSet\")\n        }\n        didSet(oldValue)\n    }\n}\n```\n\n## License\nAssociatedObject is released under the MIT License. See [LICENSE](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fp-x9%2Fassociatedobject","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fp-x9%2Fassociatedobject","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fp-x9%2Fassociatedobject/lists"}