Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vincent-pradeilles/weakable-self
A Swift micro-framework to easily deal with weak references to self inside closures
https://github.com/vincent-pradeilles/weakable-self
closures ios memory-management swift syntax
Last synced: about 2 months ago
JSON representation
A Swift micro-framework to easily deal with weak references to self inside closures
- Host: GitHub
- URL: https://github.com/vincent-pradeilles/weakable-self
- Owner: vincent-pradeilles
- License: mit
- Created: 2018-10-04T19:39:11.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-01-29T09:57:36.000Z (almost 4 years ago)
- Last Synced: 2024-04-30T06:41:43.748Z (9 months ago)
- Topics: closures, ios, memory-management, swift, syntax
- Language: Swift
- Size: 14.6 KB
- Stars: 78
- Watchers: 3
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-ios - WeakableSelf - A Swift micro-framework to encapsulate `[weak self]` and `guard` statements within closures. (Code Quality)
- awesome-swift - WeakableSelf - A micro-framework to encapsulate [weak self] and guard statements within closures. (Libs / Utility)
- awesome-swift - WeakableSelf - A micro-framework to encapsulate [weak self] and guard statements within closures. (Libs / Utility)
- awesome-ios-star - WeakableSelf - A Swift micro-framework to encapsulate `[weak self]` and `guard` statements within closures. (Code Quality)
- fucking-awesome-swift - WeakableSelf - A micro-framework to encapsulate [weak self] and guard statements within closures. (Libs / Utility)
- fucking-awesome-ios - WeakableSelf - A Swift micro-framework to encapsulate `[weak self]` and `guard` statements within closures. (Code Quality)
- fucking-awesome-ios - WeakableSelf - A Swift micro-framework to encapsulate `[weak self]` and `guard` statements within closures. (Code Quality)
- awesome-swift - WeakableSelf - A Swift micro-framework to easily deal with weak references to self inside closures ` 📝 5 months ago ` (Utility [🔝](#readme))
README
# WeakableSelf
![platforms](https://img.shields.io/badge/platforms-iOS%20%7C%20macOS%20%7C%20tvOS%20%7C%20watchOS-333333.svg)
![pod](https://img.shields.io/cocoapods/v/WeakableSelf.svg)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)## 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:
```swift
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:
```swift
import WeakableSelfservice.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"`