A Swift micro-framework to easily deal with weak references to self inside closures

Overview

WeakableSelf

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"

You might also like...
WhatsNewKit enables you to easily showcase your awesome new app features.
WhatsNewKit enables you to easily showcase your awesome new app features.

WhatsNewKit enables you to easily showcase your awesome new app features. It's designed from the ground up to be fully customized to your needs. Featu

A library that helps developers to easily perform file-related operations In iOS

File Operations Preview A library that helps developers to easily perform file-related operations. In iOS, We write our files mainly into three direct

Monitor iOS app version easily.

AppVersionMonitor Monitor iOS app version easily. You can get previous version and installation history. Usage To run the example project, clone the r

A way to easily add Cocoapod licenses and App Version to your iOS App using the Settings Bundle
A way to easily add Cocoapod licenses and App Version to your iOS App using the Settings Bundle

EasyAbout Requirements: cocoapods version 1.4.0 or above. Why you should use Well, it is always nice to give credit to the ones who helped you 😃 Bonu

 Zip - A Swift framework for zipping and unzipping files. Simple and quick to use. Built on top of minizip.
Zip - A Swift framework for zipping and unzipping files. Simple and quick to use. Built on top of minizip.

Zip A Swift framework for zipping and unzipping files. Simple and quick to use. Built on top of minizip. Usage Import Zip at the top of the Swift file

💻 A fast and flexible O(n) difference algorithm framework for Swift collection.
💻 A fast and flexible O(n) difference algorithm framework for Swift collection.

A fast and flexible O(n) difference algorithm framework for Swift collection. The algorithm is optimized based on the Paul Heckel's algorithm. Made wi

RandomKit is a Swift framework that makes random data generation simple and easy.
RandomKit is a Swift framework that makes random data generation simple and easy.

RandomKit is a Swift framework that makes random data generation simple and easy. Build Status Installation Compatibility Swift Package Manager CocoaP

An extensible monitoring framework written in Swift

XestiMonitors Overview Reference Documentation Requirements Installation CocoaPods Carthage Swift Package Manager Usage Core Location Monitors Core Mo

❇️ Feature flagging framework in Swift sauce
❇️ Feature flagging framework in Swift sauce

RealFlags makes it easy to configure feature flagsin your codebase. It's designed for Swift and provides a simple and elegant abstraction layer over m

Comments
  • Order issue

    Order issue

    Hi! Thanks for this great implementation!

    Although, its usage really looks like another operator that can be found in RxSwiftExt (I don't know if you ever heard of if :trollface: ) and the order in the arguments isn't consistent between the two methods

    for now it takes two parametters, first the passed parameter and then the self which results in a call like this :

       .subscribe(onNext: weakify { data, strongSelf in
                 strongSelf.doSomething(with: data)
                })
    

    I find it kind of awkward considering that when we use .withUnretained(self) the order is reversed.

    .withUnretained(self)
    .subscribe(onNext: { strongSelf, data in
                 strongSelf.doSomething(with: data)
                })
    

    Also in the general case in closures the compiler enforces the [weak/unretained self] to be before the arguments of the closure

    .subscribe(onNext: { [unretained self] data in
                 self.doSomething(with: data)
                })
    

    what do you think about changing the order in the new method ? If you agree I can obviously make a pull request with the changes :)

    Cheers!

    opened by HugoSay 0
Owner
Vincent Pradeilles
French iOS software engineer, working in Lyon, France 🇫🇷
Vincent Pradeilles
Swifty closures for UIKit and Foundation

Closures is an iOS Framework that adds closure handlers to many of the popular UIKit and Foundation classes. Although this framework is a substitute f

Vinnie Hesener 1.7k Dec 21, 2022
A result builder that allows to define shape building closures

ShapeBuilder A result builder implementation that allows to define shape building closures and variables. Problem In SwiftUI, you can end up in a situ

Daniel Peter 47 Dec 2, 2022
Extensions which helps to convert objc-style target/action to swifty closures

ActionClosurable Usage ActionClosurable extends UIControl, UIButton, UIRefreshControl, UIGestureRecognizer and UIBarButtonItem. It helps writing swift

takasek 121 Aug 11, 2022
Pigeon is a SwiftUI and UIKit library that relies on Combine to deal with asynchronous data.

Pigeon ?? Introduction Pigeon is a SwiftUI and UIKit library that relies on Combine to deal with asynchronous data. It is heavily inspired by React Qu

Fernando Martín Ortiz 369 Dec 30, 2022
A Swift micro library for generating Sunrise and Sunset times.

Solar A Swift helper for generating Sunrise and Sunset times. Solar performs its calculations locally using an algorithm from the United States Naval

Chris Howell 493 Dec 25, 2022
A Swift package for rapid development using a collection of micro utility extensions for Standard Library, Foundation, and other native frameworks.

ZamzamKit ZamzamKit is a Swift package for rapid development using a collection of micro utility extensions for Standard Library, Foundation, and othe

Zamzam Inc. 261 Dec 15, 2022
Framework for easily parsing your JSON data directly to Swift object.

Server sends the all JSON data in black and white format i.e. its all strings & we make hard efforts to typecast them into their respective datatypes

Mukesh 11 Oct 17, 2022
Transform strings easily in Swift.

swift-string-transform Transform strings easily in Swift. Table of Contents Installation How to use Contribution Installation Swift Package Manager (R

null 18 Apr 21, 2022
FeatureFlags.swift - Tools for easily defining feature flags for your projects

FeatureFlags.swift Tools for easily defining feature flags for your projects Int

Eric Rabil 1 Jan 31, 2022
FluxCapacitor makes implementing Flux design pattern easily with protocols and typealias.

FluxCapacitor makes implementing Flux design pattern easily with protocols and typealias. Storable protocol Actionable protocol Dispatch

Taiki Suzuki 123 Aug 23, 2022