Weakable 1.0.1

Weakable 1.0.1

Bell App Lab维护。




Weakable 1.0.1

Weakable 版本 许可

Platforms Swift support CocoaPods Compatible Carthage compatible Swift Package Manager compatible Twitter

Weakable

Weakable是一种在Swift中持有所需weak引用的简单方法。

使用Weakable,您可以创建弱数组、弱字典以及其他许多有趣的功能。

😎

需求

  • iOS 9+
  • watchOS 3+
  • tvOS 9+
  • macOS 10.11+
  • Swift 3.3+

使用说明

以两种方式之一声明您的Weak变量

//Given a class
class TestClass {}
//and an instance of that class
var aTestObject = TestClass()

//You can create a Weak like this:
var weakTestObject = Weak(aTestObject)

//Or using the shorthand operator ≈
var anotherWeakTestObject = test

访问您的变量

weakTestObject.object //returns your value as an optional, since it may or may not have been released

运算符

Weakable提供了3个运算符,都是使用字符 (⌥ + x)。

  • 前缀 ≈
    • Weak变量的简写构造函数
//Given an object
let object = AwesomeClass()

//you can create a Weak by either
var weakObject = Weak(object)

//or
var weakObject = object
  • 后缀运算符 ≈
    • Weak的简写访问器
//Given a Weak
var weakObject = object

//you can access the underlying object by
weakObject.object

//or
weakObject
  • 中缀运算符 ≈
    • Weak的简写赋值
//Given a Weak
var weakObject = object

//you can change the underlying object by
weakObject.object = anotherObject

//or
weakObject  anotherObject

数组和字典

您可以将 Weak 变量安全地存储在集合中(例如:[Weak])。底层对象不会被保留。

var tests = (1...10).map { TestClass() } // 10 elements
var weakTests = tests.map { $0 } // 10 elements

tests.removeLast() // `tests` now have 9 elements, but `weakTests` have 10

weakTests = weakTests.filterWeaks() // `weakTests` now have 9 elements too, since we dropped the released objects from it

您还可以快速“展开”Weak 集合中的元素。

let tests = weakTests.compactWeaks()

现在变量 tests 将是一个仅包含还未释放元素的 [TestClass] 集合。

安装

Cocoapods

pod 'Weakable', '~> 1.0'

然后在需要的地方导入 Weakable

Carthage

github "BellAppLab/Weakable" ~> 1.0

然后在需要的地方导入 Weakable

Swift 包管理器

dependencies: [
    .package(url: "https://github.com/BellAppLab/Weakable", from: "1.0")
]

然后在需要的地方导入 Weakable

Git 子模块

cd toYourProjectsFolder
git submodule add -b submodule --name Weakable https://github.com/BellAppLab/Weakable.git

然后将 Weakable 文件夹拖到您的 Xcode 项目中。

作者

贝尔应用实验室,[email protected]

致谢

Logo 图片Артур Абт 提供,来自 The Noun Project

许可

Weakable 可在 MIT 许可下使用。有关更多信息,请参阅 LICENSE 文件。