{"id":21064607,"url":"https://github.com/capturecontext/swift-foundation-extensions","last_synced_at":"2026-02-07T17:01:03.233Z","repository":{"id":107402048,"uuid":"482592223","full_name":"CaptureContext/swift-foundation-extensions","owner":"CaptureContext","description":"Standard extensions for Foundation framework","archived":false,"fork":false,"pushed_at":"2026-02-04T14:44:55.000Z","size":101,"stargazers_count":19,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-02-04T21:52:14.618Z","etag":null,"topics":["associated-objects","associatedobject","async","coding","essentials","extensions","foundation","macros","runtime","spm","swift","swift-macro","swift-macros","swift-package-manager","swizzling","undo-redo"],"latest_commit_sha":null,"homepage":"https://swiftpackageindex.com/CaptureContext/swift-foundation-extensions/0.4.2/documentation/foundationextensions","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/CaptureContext.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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2022-04-17T17:42:17.000Z","updated_at":"2026-02-04T14:50:19.000Z","dependencies_parsed_at":"2023-12-16T02:29:01.341Z","dependency_job_id":"d81b4edd-3825-4fb5-8f45-8072579d9ab4","html_url":"https://github.com/CaptureContext/swift-foundation-extensions","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"purl":"pkg:github/CaptureContext/swift-foundation-extensions","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CaptureContext%2Fswift-foundation-extensions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CaptureContext%2Fswift-foundation-extensions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CaptureContext%2Fswift-foundation-extensions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CaptureContext%2Fswift-foundation-extensions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CaptureContext","download_url":"https://codeload.github.com/CaptureContext/swift-foundation-extensions/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CaptureContext%2Fswift-foundation-extensions/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29200843,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-07T16:28:23.579Z","status":"ssl_error","status_checked_at":"2026-02-07T16:28:22.566Z","response_time":63,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["associated-objects","associatedobject","async","coding","essentials","extensions","foundation","macros","runtime","spm","swift","swift-macro","swift-macros","swift-package-manager","swizzling","undo-redo"],"created_at":"2024-11-19T17:50:14.890Z","updated_at":"2026-02-07T17:00:58.192Z","avatar_url":"https://github.com/CaptureContext.png","language":"Swift","readme":"# swift-foundation-extensions\n\n[![CI](https://github.com/CaptureContext/swift-foundation-extensions/actions/workflows/ci.yml/badge.svg)](https://github.com/CaptureContext/swift-foundation-extensions/actions/workflows/ci.yml) [![SwiftPM 5.9](https://img.shields.io/badge/swiftpm-5.9-ED523F.svg?style=flat)](https://swift.org/download/) ![Platforms](https://img.shields.io/badge/Platforms-iOS_13_|_macOS_10.15_|_tvOS_14_|_watchOS_7-ED523F.svg?style=flat) [![@capture_context](https://img.shields.io/badge/contact-@capturecontext-1DA1F2.svg?style=flat\u0026logo=twitter)](https://twitter.com/capture_context) \n\nStandard extensions for Foundation framework\n\n- [Documentation](https://swiftpackageindex.com/CaptureContext/swift-foundation-extensions/0.5.0/documentation/foundationextensions)\n- [Contents](#contents)\n  - [Coding](#coding)\n  - [NSLocking](#nslocking)\n  - [Optional](#optional)\n  - [Undo/Redo management](#undoredo-management)\n  - [Indirect](#indirect)\n  - [Property Proxy](#property-proxy)\n  - [Object Association](#object-Association)\n  - [Swizzling](#swizzling)\n- [Installation](#installation)\n  - [Basic](#basic)\n  - [Recommended](#recommended)\n- [Licence](#licence)\n\n## Contents\n\n### Coding\n\n- RawCodingKey allows you to create CodingKeys from literals\n\n- Extensions for encoder and decoder allow you to create an object with a contextual container\n\n- Extensions for coding containers automatically infer type from context\n\n```swift\ninit(from decoder: Decoder) throws {\n  self = try container.decode(RawCodingKey.self) { container in\n    return .init(\n      someProperty1: container.decode(\"someProperty1\"),\n      someProperty2: container.decode(\"some_property_2\")\n    )\n  }\n}\n\nfunc encode(to encoder: encoder) throws {\n  try encoder.encode(RawCodingKey.self) { container in\n    try container.encode(someProperty1, forKey: \"someProperty1\")\n    try container.encode(someProperty2, forKey: \"some_property_2\")\n  }\n}\n```\n\n### NSLocking\n\n- `store(_:in:)` - stores value in some variable in locked context\n- `mutate(_:with:)` - passes given object to locked context\n- `assign(_:to:on:)` - stores value in object property in locked context\n- `execute(_:)` - provides new locked context\n\n### Optional\n\n- `orThrow(_:)` - unwraps an optional or throws specified error\n- `isNil` / `isNotNil` / `isNilOrEmpty`\n- `or()` - coalesing alias\n- `unwrap()` - returns unwrapping Result\n- `assign(to:on:)` - assigns wrapped value to a specified target property by the keyPath\n- `ifLetAssign(to:on:)` - assigns wrapped value to a specified target property by the keyPath if an optional was not nil\n\n### Undo/Redo management\n\n```swift\nstruct State {\n  var value: Int = 0\n}\n\n@Resettable\nlet state = State()\nstate.value = 1   // value == 1\nstate.value *= 10 // value == 10\nstate.undo()      // value == 1\nstate.value += 1  // value == 2\nstate.undo()      // value == 1\nstate.redo()      // value == 2\n```\n\n### Indirect\n\nCoW container, which allows you to recursively include single instances of value types\n\n```swift\nstruct ListNode\u003cValue\u003e {\n  var value: Value\n  \n\t@Indirect\n  var next: ListNode\u003cValue\u003e?\n}\n```\n\n\n\n### PropertyProxy\n\n```swift\nclass MyView: UIView {\n  private let label: UILabel\n  \n  @PropertyProxy(\\MyView.label.text)\n  var text: String?\n}\n\nlet view: MyView = .init()\nview.label.text // ❌\nview.text = \"Hello, World!\"\n```\n\n### Object Association\n\nBasic helpers for object association are available in a base package\n\n```swift\nextension UIViewController {\n  var someStoredProperty: Int {\n    get { getAssociatedObject(forKey: #function).or(0) }\n    set { setAssociatedObject(newValue, forKey: #function)  }\n  }\n}\n\nlet value: Bool = getAssociatedObject(forKey: \"value\", from: object)\n```\n\nBut the full power of associated objects is provided by `FoundationExtensionsMacros` target\n\n\u003e By default `@AssociatedObject` macro uses `.retain(.nonatomic)` for classes and `.copy(.nonatomic)` `objc_AssociationPolicy` for structs.\n\n```swift\nimport FoundationExtensionsMacros\n\nextension SomeClass {\n  @AssociatedObject\n  var storedVariableInExtension: Int = 0\n  \n  @AssociatedObject(readonly: true)\n  var storedVariableInExtension: SomeObject = .init()\n  \n  @AssociatedObject\n  var optionalValue: Int?\n  \n  @AssociatedObject\n  var object: Int?\n    \n  @AssociatedObject(threadSafety: .atomic)\n  var threadSafeValue: Int?\n    \n  @AssociatedObject(threadSafety: .atomic)\n  var threadSafeObject: Object?\n    \n  @AssociatedObject(policy: .assign)\n  var customPolicyValue: Int?\n    \n  @AssociatedObject(policy: .retain(.atomic))\n  var customPolicyThreadSafeObject: Object?\n}\n```\n\n\u003e Macros require swift-syntax compilation, so it will affect cold compilation time\n\n### Swizzling\n\nThis package also provides some sugar for objc method swizzling\n\n```swift\nextension UIViewController {\n  // Runs once in app lifetime\n  // Repeated calls do nothing\n  private static let swizzle: Void = {\n    // This example is not really representative since these methods\n    // can be simply globally overriden, but it's just an example\n    // for the readme and you can find live example at\n    // https://github.com/capturecontext/combine-cocoa-navigation\n    \n    objc_exchangeImplementations(\n    \t#selector(viewWillAppear)\n      #selector(__swizzledViewWillAppear)\n    )\n    \n    objc_exchangeImplementations(\n    \t#selector(viewDidAppear)\n      #selector(__swizzledViewDidAppear)\n    )\n  }()\n\n  @objc dynamic\n  private func __swizzledViewWillAppear(_ animated: Bool) {\n    __swizzledViewWillAppear(animated) // calls original method\n    print(type(of: self), ObjectIdentifier(self), \"will appear\")\n  }\n\n  @objc dynamic\n  private func __swizzledViewDidAppear(_ animated: Bool) {\n    __swizzledViewDidAppear(animated) // calls original method\n    print(type(of: self), ObjectIdentifier(self), \"did appear\")\n  }\n}\n```\n\n## Installation\n\n### Basic\n\nYou can add FoundationExtensions to an Xcode project by adding it as a package dependency.\n\n1. From the **File** menu, select **Swift Packages › Add Package Dependency…**\n2. Enter [`\"https://github.com/capturecontext/swift-foundation-extensions.git\"`](https://github.com/capturecontext/swift-foundation-extensions.git) into the package repository URL text field\n3. Choose products you need to link them to your project.\n\n### Recommended\n\nIf you use SwiftPM for your project, you can add StandardExtensions to your package file.\n\n```swift\n.package(\n  url: \"https://github.com/capturecontext/swift-foundation-extensions.git\", \n  .upToNextMinor(from: \"0.5.0\")\n)\n```\n\nDo not forget about target dependencies:\n\n```swift\n.product(\n  name: \"FoundationExtensions\", \n  package: \"swift-foundation-extensions\"\n)\n```\n\n```swift\n.product(\n  name: \"FoundationExtensionsMacros\", \n  package: \"swift-foundation-extensions\"\n)\n```\n\n\n\n## License\n\nThis library is released under the MIT license. See [LICENCE](LICENCE) for details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcapturecontext%2Fswift-foundation-extensions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcapturecontext%2Fswift-foundation-extensions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcapturecontext%2Fswift-foundation-extensions/lists"}