A simple solution for tiring [weak self] in swift

Overview

Weakifiable

A simple solution for tiring [weak self] in swift

Usage

  1. give this repo a Star ⭐️
  2. Add EBWeakifiable to your Project.
  3. simply after each closure add weakify like sample below:
producer.register(handler: weakify { strongSelf, result in
            strongSelf.handle(result)
 })

to show case the full ability here is a sampleCode of both producing and consuming:

Bool in return strongSelf.login(number) }) } // for 3 private func handle(_ result: Int) { print("🎉 \(result)") } // for 4 private func login(_ result: Int) -> Bool { print("🎉 \(result) is Login") return true } // for 2 private func signout() -> Bool { print("🎉 user is signout") return true } // for 1 private func runJob() { print("🎉 run Job") } deinit { print("deinit Consumer") } } ">
class User: NSObject {
    
    private var handler: (Int) -> Void = { _ in }
    
    func register(handler: @escaping (Int) -> Void) {
        self.handler = handler
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: { self.handler(42) })
    }
    
    func login(handler: @escaping (Int) -> Bool?) {
        
    }
    
    func signout(handler: @escaping () -> Bool?) {
        
    }
    
    func runJob(handler: @escaping () -> Void) {
        
    }
    
    deinit {
        print("deinit Producer")
    }
    
}

class Consumer: NSObject {
    
    let user = User()
    
    func consume() {
        
        // MARK: - type 1
        /// old
        user.runJob { [weak self] in
             guard let self = self else { return }
             self.runJob()
         }
        
        /// new
        user.runJob(handler: weakify { strongSelf in
            strongSelf.runJob()
        })
        
        // MARK: - type 2
        /// old
        user.signout { [weak self] in
             guard let self = self else { return false }
             return self.signout()
         }
        
        /// new
        user.signout(handler: weakify { strongSelf in
            return strongSelf.signout()
        })
        
        // MARK: - type 3
        /// old
        user.register { [weak self] result in
             guard let self = self else { return }
             self.handle(result)
         }
        
        /// new
        user.register(handler: weakify { strongSelf, result in
            strongSelf.handle(result)
        })
        
        // MARK: - type 4
        /// old
        user.login { [weak self] number in
             guard let self = self else { return false }
             return self.login(number)
         }
        
        /// new
        user.login(handler: weakify { (strongSelf, number) -> Bool in
            return strongSelf.login(number)
        })
        
    }
    
    // for 3
    private func handle(_ result: Int) {
        print("🎉 \(result)")
    }
    
    // for 4
    private func login(_ result: Int) -> Bool {
        print("🎉 \(result) is Login")
        return true
    }
    
    // for 2
    private func signout() -> Bool {
        print("🎉 user is signout")
        return true
    }
    
    // for 1
    private func runJob() {
        print("🎉 run Job")
    }
    
    deinit {
        print("deinit Consumer")
    }
}
You might also like...
Simple and Lightweight App Version Tracking for iOS written in Swift

AEAppVersion Simple and lightweight iOS App Version Tracking written in Swift I made this for personal use, but feel free to use it or contribute. For

Simple getter for bundle informations, written in Swift

BundleInfos Simple getter for bundle informations It's simple and static way for getting information from main bundle Requirements iOS 8.0+ Xcode 7.3

Async+ for Swift provides a simple chainable interface for your async and throwing code, similar to promises and futures
Async+ for Swift provides a simple chainable interface for your async and throwing code, similar to promises and futures

Async+ for Swift provides a simple chainable interface for your async and throwing code, similar to promises and futures. Have the best of both worlds

A simple utility allowing to detect Swift version at runtime.

SwiftVersionDetector SwiftVersionDetector allows you to detect Swift version at runtime. Note that detecting the Swift version of the machine on which

Diff - Simple diffing library in pure Swift

Diff Simple diffing library in pure Swift. Installing You can use Carthage or Swift Package Manager to install Diff. Usage Start by importing the pack

Updeto is a simple package that help update checker for iOS Apps

Updeto is a simple package that will help you to check if the currently installed version is the same as the latest one available on App Store.

A simple shake-one-shake, Convenient for us to integrate the shake-one-shake.

MGSwiftShaker Example To run the example project, clone the repo, and run pod install from the Example directory first.

Simple and blunt static site generator

StaticSite This contains a bunch of helper functions to generate a static site in Swift.

Simple way to set up half modal view
Simple way to set up half modal view

HalfModalView Requirements iOS 9.0+ Xcode 10.0+ Swift 4.0+ Example Installation CocoaPods is a dependency manager for Cocoa projects. You can install

Owner
Emad Beyrami
iOS Developer @BornakGmbH
Emad Beyrami
A simple swift package that provides a Swift Concurrency equivalent to `@Published`.

AsyncValue This is a simple package that provides a convenience property wrapper around AsyncStream that behaves almost identically to @Published. Ins

Brent Mifsud 33 Oct 3, 2022
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

Roy Marmelstein 2.3k Jan 3, 2023
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

Nikolai Vazquez 1.5k Dec 29, 2022
A simple Pokedex app written in Swift that implements the PokeAPI, using Combine and data driven UI.

SwiftPokedex SwiftPokedex is a simple Pokedex app written by Viktor Gidlöf in Swift that implements the PokeAPI. For full documentation and implementa

Viktor G 26 Dec 14, 2022
A simple Swift package for counting the Syllables in a sentence.

A simple Swift package for counting the Syllables in a sentence.

null 2 Jan 3, 2022
A simple Swift utility for producing pseudolocalized strings.

Build your App UI to adapt and respond to translations, and find localization bugs!

Reece Como 2 Sep 28, 2021
AnalyticsKit for Swift is designed to combine various analytical services into one simple tool.

?? AnalyticsKit AnalyticsKit for Swift is designed to combine various analytical services into one simple tool. To send information about a custom eve

Broniboy 6 Jan 14, 2022
Angle is a simple Swift library that provides Angle structure representing angles.

Angle is a simple Swift library that provides Angle structure representing angles. It handles angles using circular measure by default but is al

Geonu Jeon 2 Nov 30, 2021
A simple, but efficient CSV Parser, written in Swift.

CSV CSV.swift is a powerful swift library for parsing CSV files that supports reading as [String], [String: String] and Decodable, without sacrificing

Ben Koska 4 Nov 28, 2022
DeviceGuru is a simple lib (Swift) to know the exact type of the device

DeviceGuru DeviceGuru is a simple lib (Swift) to know the exact type of the device, e.g. iPhone 6 or iPhone 6s Easy to use Light weight From version 5

Inder Kumar 442 Dec 28, 2022