swift-capture

test SwiftPM 5.9 Platforms docs @capture_context

一个在 Swift 中符合人体工程学且安全地捕获和弱化对象的机制。

使用示例

Without Capture
With Capture

默认

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

多个参数

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

方法

{ [weak self] in 
  guard let self = self else { return }
  self.someMethod()
}
capture(in: <#Type#>.someMethod)

返回值

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

安装

基本

你可以通过将其添加为包依赖项,将 swift-capture 添加到 Xcode 项目中。

  1. 文件菜单中,选择 Swift Packages › Add Package Dependency…
  2. 在包仓库 URL 文本字段中输入 "https://github.com/capturecontext/swift-capture"
  3. 选择你需要的产品,将它们链接到你的项目。

推荐

如果你的项目使用 SwiftPM,你可以将 weak 添加到你的包文件中。我的建议是使用 SSH。

.package(
  url: "git@github.com:capturecontext/swift-capture.git",
  .upToNextMajor("3.0.0")
)

不要忘记目标依赖项

.product(
    name: "Capture", 
    package: "swift-capture"
)

许可证

此库在 MIT 许可证下发布。有关详细信息,请参阅 LICENSE