weak

main

A mechanism for safe capturing & weakifying objects in Swift.
maximkrouk/weak

Weak

SwiftPM 5.3 @maximkrouk

A mechanism for safe capturing & weakifying objects in Swift.

Usage Examples

Without weak
With weak

Default

{ [weak self] in 
  guard let self = self else { return }
  /// ...
}
capture { _self in
  /// ...
}

Multiple parameters

{ [weak self] a, b, c in 
  guard let self = self else { return }
  /// ...
}
capture { _self, a, b, c in 
  /// ...
}

Methods

{ [weak self] in 
  guard let self = self else { return }
  self.someMethod()
}
capture(in: Self.someMethod)

Return values

let object.dataSource = { [weak self] in
  guard let self = self else { return [] }
  return self.data
}
let object.dataSource = capture(or: [], in: \.data)

Installation

Basic

You can add weak 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/makeupstdio/weak" 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.

.package(
  name: "weak",
  url: "git@github.com:makeupstudio/weak.git",
  .branch("main")
)

Do not forget about target dependencies:

.product(
    name: "Weak", 
    package: "weak"
)

License

This library is released under the MIT license. See LICENSE for details.

Description

  • Swift Tools 5.3.0
View More Packages from this Author

Dependencies

  • None
Last updated: Thu Mar 14 2024 20:43:27 GMT-0900 (Hawaii-Aleutian Daylight Time)