Git Product home page Git Product logo

delegated's Introduction

Delegated

Swift Platform

Delegated is a super small package that solves the retain cycle problem when dealing with closure-based delegation.

Medium post here.

Usage

Before:

self.downloader = ImageDownloader()
downloader.didDownload = { [weak self] image in
    guard let strongSelf = self else {
        return
    }
    strongSelf.currentImage = image
}

After:

self.downloader = ImageDownloader()
downloader.didDownload.delegate(to: self) { (self, image) in
    self.currentImage = image
}

No retain cycles! No memory leaks! No [weak self]! πŸŽ‰

Guide

Creating a delegated function

class NumberPrinter {
    var numberToString = Delegated<Int, String>()
}

Registering as a delegate

// somewhere inside init() or viewDidLoad() or similar
self.printer = NumberPrinter()
printer.numberToString.delegate(to: self) { (self, number) -> String in
    return self.convert(number: number)
}

By default, delegate(to:with:) will wrap self in a weak reference so that you don't need to remember to write [weak self] every time. This is the main feature of Delegated, but if this is not the behavior you want, you can use stronglyDelegated(to:with:) or manuallyDelegated(with:):

self.printer = NumberPrinter()
printer.numberToString.stronglyDelegate(to: self) { (self, number) -> String in
    // will hold a strong reference to  `self`
    return self.convert(number: number) 
}
let printer = NumberPrinter()
printer.numberToString.manuallyDelegate(with: { number in
    return String(number)
})

Calling a delegate

class NumberPrinter {
    
    var numberToString = Delegated<Int, String>()
    
    func printNumber(_ number: Int) {
        // .call returns nil if no delegate was set
        guard let string = numberToString.call(number) else {
            return
        }
        print(string)
    }
    
}

Removing a delegate

var numberToString = Delegated<Int, String>()
// ...
numberToString.removeDelegate()

Checking if delegate is set

var numberToString = Delegated<Int, String>()
// ...
numberToString.isDelegateSet // Bool

Installation

Delegated is available through Carthage. To install, just write into your Cartfile:

github "dreymonde/Delegated" ~> 0.1.0

Delegated is also available through Cocoapods:

pod 'Delegated', '~> 0.1.0'

And Swift Package Manager:

dependencies: [
    .Package(url: "https://github.com/dreymonde/Delegated.git", majorVersion: 0, minor: 1),
]

And, of course, you always have an option of just copying-and-pasting the code - Delegated is just one file, so feel free.

See also

delegated's People

Contributors

dreymonde avatar

Watchers

James Cloos avatar meng_xing_dong avatar

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.