Delegated is a super small package that solves the retain cycle problem when dealing with closure-based delegation.
Medium post here.
self.downloader = ImageDownloader()
downloader.didDownload = { [weak self] image in
self?.currentImage = image
}
self.downloader = ImageDownloader()
downloader.didDownload.delegate(to: self) { (self, image) in
self.currentImage = image
}
No retain cycles! No memory leaks! No [weak self]
! 🎉
class NumberPrinter {
var numberToString = Delegated<Int, String>()
}
// 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)
})
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)
}
}
var numberToString = Delegated<Int, String>()
// ...
numberToString.removeDelegate()
var numberToString = Delegated<Int, String>()
// ...
numberToString.isDelegateSet // Bool
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.
- krzysztofzablocki/Strongify - a 1-file µframework providing a nicer API for avoiding weak-strong dance.