Giter Site home page Giter Site logo

vincent-pradeilles / weakable-self Goto Github PK

View Code? Open in Web Editor NEW
78.0 3.0 6.0 15 KB

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

License: MIT License

Objective-C 2.10% Swift 89.98% Ruby 7.92%
ios swift closures memory-management syntax

weakable-self's Introduction

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"

weakable-self's People

Contributors

fassko avatar hugosay avatar vincent-pradeilles avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

weakable-self's Issues

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.