项目作者: BellAppLab

项目描述 :
An easy way to hold weak references in Swift.
高级语言: Swift
项目地址: git://github.com/BellAppLab/Weakable.git
创建时间: 2018-07-12T06:42:08Z
项目社区:https://github.com/BellAppLab/Weakable

开源协议:MIT License

下载


Weakable Version License

Platforms
Swift support
Swift Package Manager compatible

Weakable

Weakable is an easy way to hold weak references in Swift.

With Weakable you can create weak arrays, weak dictionaries and many other cool things.

😎

Requirements

  • iOS 12+
  • watchOS 4+
  • tvOS 12+
  • macOS 11+
  • Swift 4+

Usage

Declare your Weak variable in one of the two ways provided:

  1. //Given a class
  2. class TestClass {}
  3. //and an instance of that class
  4. var aTestObject = TestClass()
  5. //You can create a Weak like this:
  6. var weakTestObject = Weak(aTestObject)
  7. //Or using the shorthand operator ≈
  8. var anotherWeakTestObject = test

Access your variable:

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

Operators

Weakable comes with 3 operators, all using the character (⌥ + x).

  • prefix ≈
    • Shorthand contructor for a Weak variable:
  1. //Given an object
  2. let object = AwesomeClass()
  3. //you can create a Weak by either
  4. var weakObject = Weak(object)
  5. //or
  6. var weakObject = object
  • postfix operator ≈
    • Shorthand accessor for Weak:
  1. //Given a Weak
  2. var weakObject = object
  3. //you can access the underlying object by
  4. weakObject.object
  5. //or
  6. weakObject
  • infix operator ≈
    • Shorthand assignment for Weak:
  1. //Given a Weak
  2. var weakObject = object
  3. //you can change the underlying object by
  4. weakObject.object = anotherObject
  5. //or
  6. weakObject anotherObject

Arrays and Dictionaries

You can safely store your Weak variables in collections (eg. [Weak<TestClass>]). The underlaying objects won’t be retained.

  1. var tests = (1...10).map { TestClass() } // 10 elements
  2. var weakTests = tests // 10 elements
  3. tests.removeLast() // `tests` now have 9 elements, but `weakTests` have 10
  4. weakTests = weakTests.filterWeaks() // `weakTests` now have 9 elements too, since we dropped the released objects from it

You can also quickly “unwrap” the elements in a Weak collection:

  1. let tests = weakTests.compactWeaks()

The variable tests will now be a [TestClass] containing only the elements that haven’t been released yet.

Global weaks

Version 2.0 introduces the concept of global weak variables. Say you want to share the same instance of a class in several places, but you want to release the global variable once all other references are destroyed. That’s what a global weak is.

  1. final class TestClass: WeaklyGloballyIdentifiable {
  2. typealias GlobalID = Int
  3. @WeakGlobalActor
  4. static var weakGlobals: [GlobalID: Weak<TestClass>] = [:]
  5. }
  6. let firstInstance = TestClass.weakGlobal(id: 1, default: TestClass())
  7. let secondInstance = TestClass.weakGlobal(id: 1, default: TestClass())
  8. print(firstInstance === secondInstance) // `true`

Installation

Swift Package Manager

  1. dependencies: [
  2. .package(url: "https://github.com/BellAppLab/Weakable", from: "2.0")
  3. ]

Then import Weakable where needed.

Legacy

The following import methods only work for version 1.0.1.

Cocoapods

  1. pod 'Weakable', '~> 1.0'

Then import Weakable where needed.

Carthage

  1. github "BellAppLab/Weakable" ~> 1.0

Then import Weakable where needed.

Git Submodules

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

Then drag the Weakable folder into your Xcode project.

Author

Bell App Lab, apps@bellapplab.com

Credits

Logo image by Артур Абт from The Noun Project

License

Weakable is available under the MIT license. See the LICENSE file for more info.