https://github.com/capturecontext/swift-capture
A mechanism for safe capturing & weakifying objects in Swift.
https://github.com/capturecontext/swift-capture
arc memory-management spm swift swift-package-manager
Last synced: 4 months ago
JSON representation
A mechanism for safe capturing & weakifying objects in Swift.
- Host: GitHub
- URL: https://github.com/capturecontext/swift-capture
- Owner: CaptureContext
- License: mit
- Created: 2021-08-07T12:29:10.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-09-21T19:15:17.000Z (over 1 year ago)
- Last Synced: 2025-10-02T03:59:48.419Z (4 months ago)
- Topics: arc, memory-management, spm, swift, swift-package-manager
- Language: Swift
- Homepage: https://swiftpackageindex.com/CaptureContext/swift-capture/3.0.1/documentation
- Size: 34.2 KB
- Stars: 7
- Watchers: 0
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# swift-capture
[](https://github.com/CaptureContext/swift-capture/actions/workflows/Test.yml) [](https://github.com/CaptureContext/swift-declarative-configuration/actions/workflows/Test.yml) 
[]([https://twitter.com/capture_context](https://swiftpackageindex.com/CaptureContext/swift-capture/3.0.1/documentation)) [](https://twitter.com/capture_context)
A mechanism for ergonomic and safe capturing & weakifying objects in Swift.
## Usage Examples
```swift
Without Capture
```
```swift
With Capture
```
----
Default
```swift
{ [weak self] in
guard let self else { return }
/// ...
}
```
```swift
capture { _self in
/// ...
}
```
----
Multiple parameters
```swift
{ [weak self] a, b, c in
guard let self else { return }
/// ...
}
```
```swift
capture { _self, a, b, c in
/// ...
}
```
---
Methods
```swift
{ [weak self] in
guard let self = self else { return }
self.someMethod()
}
```
```swift
capture(in: <#Type#>.someMethod)
```
----
Return values
```swift
let object.dataSource = { [weak self] in
guard let self = self else { return [] }
return self.data
}
```
```swift
let object.dataSource = capture(orReturn: [], in: \.data)
```
## Installation
### Basic
You can add `swift-capture` to an Xcode project by adding it as a package dependency.
1. From the **File** menu, select **Swift Packages › Add Package Dependency…**
2. Enter [`"https://github.com/capturecontext/swift-capture"`](https://github.com/capturecontext/swift-capture) into the package repository URL text field
3. Choose products you need to link them to your project.
### Recommended
If you use SwiftPM for your project, you can add `weak` to your package file. Also my advice is to use SSH.
```swift
.package(
url: "git@github.com:capturecontext/swift-capture.git",
.upToNextMajor("3.0.0")
)
```
Do not forget about target dependencies:
```swift
.product(
name: "Capture",
package: "swift-capture"
)
```
## License
This library is released under the MIT license. See [LICENSE](./LICENSE) for details.