Latest 1.1.1
Homepage https://github.com/vincent-pradeilles/weakable-self
License MIT
Platforms ios 9.0, tvos 10.0, osx 10.12, watchos 3.0
Frameworks Foundation
Authors

platforms
pod
Carthage compatible

Context

Closures are one of Swift must-have features, and Swift developers are aware of how tricky they can be when they capture the reference of an external object, especially when this object is self.

To deal with this issue, developers are required to write additional code, using constructs such as [weak self] and guard, and the result looks like the following:

service.call(completion: { [weak self] result in
    guard let self = self else { return }

    // use weak non-optional `self` to handle `result`
})

Purpose of WeakableSelf

The purpose of this micro-framework is to provide the developer with a helper function weakify that will allow him to declaratively indicate that he wishes to use a weak non-optional reference to self in closure, and not worry about how this reference is provided.

Usage

Using this weakify function, the code above will be transformed into the much more concise:

import WeakableSelf

service.call(completion: weakify { strongSelf, result in    
    // use weak non-optional `strongSelf` to handle `result`
})

weakify works with closures that take up to 7 arguments.

Installation

Requirements

  • Swift 4.2+
  • Xcode 10+

CocoaPods

Add the following to your Podfile:

pod "WeakableSelf"

Carthage

Add the following to your Cartfile:

github "vincent-pradeilles/weakable-self"

Latest podspec

{
    "name": "WeakableSelf",
    "version": "1.1.1",
    "summary": "A Swift micro-framework to easily deal with weak references to self inside closures",
    "description": "Closures are one of Swift must-have features, and Swift developers are aware of how tricky they can be when they capture the reference of an external object, especially when this object is self.nnTo deal with this issue, developers are required to write additional code, using constructs such as [weak self] and guard, and the result looks like the following:nnservice.call(completion: { [weak self] result inn    guard let self = self else { return }nn    // use weak non-optional `self` to handle `result`n})nnThe purpose of this micro-framework is to provide the developer with a helper function weakify that will allow him to declaratively indicate that he wishes to use a weak non-optional reference to self in closure, and not worry about how this reference is provided.nnUsing this weakify function, the code above will be transformed into the much more concise:nnimport WeakableSelfnnservice.call(completion: weakify { result, strongSelf inn    // use weak non-optional `strongSelf` to handle `result`n})nn`weakify` works with closures that take up to 7 arguments.",
    "homepage": "https://github.com/vincent-pradeilles/weakable-self",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Vincent Pradeilles": "[email protected]"
    },
    "source": {
        "git": "https://github.com/vincent-pradeilles/weakable-self.git",
        "tag": "1.1.1"
    },
    "swift_version": "4.2",
    "platforms": {
        "ios": "9.0",
        "tvos": "10.0",
        "osx": "10.12",
        "watchos": "3.0"
    },
    "frameworks": "Foundation",
    "source_files": "Sources/**/*.swift"
}

Pin It on Pinterest

Share This