Latest 1.0.1
Homepage https://github.com/BellAppLab/Weakable
License MIT
Platforms ios 9.0, watchos 3.0, osx 10.10, tvos 9.0
Frameworks Foundation
Authors

Weakable Version License

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

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 9+
  • watchOS 3+
  • tvOS 9+
  • macOS 10.11+
  • Swift 3.3+

Usage

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

//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

Access your variable:

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:
//Given an object
let object = AwesomeClass()

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

//or
var weakObject = ≈object
  • postfix operator ≈
    • Shorthand accessor for Weak:
//Given a Weak
var weakObject = ≈object

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

//or
weakObject≈
  • infix operator ≈
    • Shorthand assignment for Weak:
//Given a Weak
var weakObject = ≈object

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

//or
weakObject ≈ anotherObject

Arrays and Dictionaries

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

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

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

let tests = weakTests.compactWeaks()

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

Installation

Cocoapods

pod 'Weakable', '~> 1.0'

Then import Weakable where needed.

Carthage

github "BellAppLab/Weakable" ~> 1.0

Then import Weakable where needed.

Swift Package Manager

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

Then import Weakable where needed.

Git Submodules

cd toYourProjectsFolder
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, [email protected]

Credits

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

License

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

Latest podspec

{
    "name": "Weakable",
    "version": "1.0.1",
    "summary": "An easy way to hold weak references in Swift.",
    "screenshots": "https://github.com/BellAppLab/Weakable/raw/master/Images/weakable.png",
    "description": "Weakable is an easy way to hold `weak` references in Swift.nnWith Weakable you can create weak arrays, weak dictionaries and many other cool things.nnud83dude0e",
    "homepage": "https://github.com/BellAppLab/Weakable",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Bell App Lab": "[email protected]"
    },
    "social_media_url": "https://twitter.com/BellAppLab",
    "platforms": {
        "ios": "9.0",
        "watchos": "3.0",
        "osx": "10.10",
        "tvos": "9.0"
    },
    "module_name": "Weakable",
    "source": {
        "git": "https://github.com/BellAppLab/Weakable.git",
        "tag": "1.0.1"
    },
    "source_files": "Sources/Weakable",
    "frameworks": "Foundation"
}

Pin It on Pinterest

Share This