× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Execute boxed generic arguments in closure or callback.

Dima Pilipenko
Apr 19, 2016
<p>Quick tip for further passing generic arguments in closure.</p> <p> </p> <p>Steps:</p> <p>1. Make generic Box class, which accepts arguments</p> <p>2. Implement Boxable protocol, where lookInside function will execute passed closure.</p> <p>3. Execute lookInside function in needed situation.</p> <p>4. Notice recognition of passed parameters in closure or callback at pre-compile stage.</p> <p> </p> <p>Example code:</p> <pre><code>NSTimer.once(3, arguments: (magicNumber: 4, text: "Passed text to closure")) { magicNumber, text in print("I have magic number: \(magicNumber) and received this: \(text)") }</code></pre> <p> </p> <p>Full code used in production: https://github.com/dimpiax/RichTimer/blob/master/Pod/Classes/RichTimer.swift</p>
comments powered by Disqus