项目作者: maximkrouk

项目描述 :
A mechanism for safe capturing & weakifying objects in Swift.
高级语言: Swift
项目地址: git://github.com/maximkrouk/Weak.git
创建时间: 2021-02-10T11:21:21Z
项目社区:https://github.com/maximkrouk/Weak

开源协议:MIT License

下载


Weak

SwiftPM 5.3 @maximkrouk

A mechanism for safe capturing & weakifying objects in Swift.

Usage Examples

  1. Without weak
  1. With weak

Default

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

Multiple parameters

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

Methods

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

Return values

  1. let object.dataSource = { [weak self] in
  2. guard let self = self else { return [] }
  3. return self.data
  4. }
  1. 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.

If you use SwiftPM for your project, you can add weak to your package file. Also my advice is to use SSH.

  1. .package(
  2. name: "weak",
  3. url: "git@github.com:makeupstudio/weak.git",
  4. .branch("main")
  5. )

Do not forget about target dependencies:

  1. .product(
  2. name: "Weak",
  3. package: "weak"
  4. )

License

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