<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Callbacks on Andrew Bancroft</title>
    <link>https://www.andrewcbancroft.com/tags/callbacks/</link>
    <description>Recent content about iOS development with Swift in Callbacks  from Andrew Bancroft.</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Mon, 15 Feb 2016 18:21:33 +0000</lastBuildDate>
    
        <atom:link href="https://www.andrewcbancroft.com/tags/callbacks/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Fundamentals of Callbacks for Swift Developers</title>
      <link>https://www.andrewcbancroft.com/2016/02/15/fundamentals-of-callbacks-for-swift-developers/</link>
      <pubDate>Mon, 15 Feb 2016 18:21:33 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2016/02/15/fundamentals-of-callbacks-for-swift-developers/</guid>
      <description>&lt;p&gt;&lt;small&gt;Updated on October 12, 2016 – Swift 3.0&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;Callbacks: What are they? How do they work? What are they used for in practice?&lt;/p&gt;
&lt;p&gt;My goal in this article is to provide answers to these questions so that you have a foundational understanding of this common programming pattern in iOS development.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;what-are-callbacks&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;what-are-callbacks&#34;&gt;What are callbacks?&lt;/h3&gt;
&lt;p&gt;Let&amp;rsquo;s approach the definition from a &amp;ldquo;big picture” scenario:&lt;/p&gt;
&lt;p&gt;When we&amp;rsquo;re building software, we&amp;rsquo;re either &lt;em&gt;using&lt;/em&gt; APIs, or &lt;em&gt;building&lt;/em&gt; APIs, are we not? We&amp;rsquo;re either &lt;em&gt;using&lt;/em&gt; code that &amp;ldquo;hooks into” what other developers have designed and made available to us, or we&amp;rsquo;re &lt;em&gt;creating&lt;/em&gt; code that other code will &amp;ldquo;hook into” and interact with, even if the &amp;ldquo;other code” is written by us in our own app.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;design-api-callbacks&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;learn-by-example-designing-an-api-for-callbacks&#34;&gt;Learn by example: Designing an API for callbacks&lt;/h4&gt;
&lt;p&gt;Since this is the case, let&amp;rsquo;s put on the &amp;ldquo;API Designer” hat for a moment and suppose that we&amp;rsquo;re working to create a hypothetical Type called an &lt;code&gt;ImageSketcher&lt;/code&gt;. One of the functions of &lt;code&gt;ImageSketcher&lt;/code&gt; is called &lt;code&gt;sketch()&lt;/code&gt; (parameters omitted for the moment). It will allow developers to pass it an image resource, such as a JPEG or a PNG, as one of its arguments. The function will then proceed to generate an animated sketch of that PNG for the user to view.&lt;/p&gt;
&lt;p&gt;In order to do the work of generating the animated sketch, &lt;code&gt;sketch()&lt;/code&gt; needs to do a lot of crunching. I have no idea what it&amp;rsquo;d take to do this in real life, honestly – let&amp;rsquo;s just work on the premise that it&amp;rsquo;ll take a few seconds to generate the animation so the end-user can watch it when it&amp;rsquo;s finished.&lt;/p&gt;
&lt;p&gt;In situations like this, it&amp;rsquo;d be nice to design &lt;code&gt;ImageSketcher&lt;/code&gt; where the start and end of the process are decoupled:&lt;/p&gt;
&lt;p&gt;Pass off the image. Let it do its thing to generate the animation. When it&amp;rsquo;s finished, &amp;ldquo;hook back in” and respond to the knowledge that the animation generation is complete. At that point, we could ask the end-user, &amp;ldquo;Hey, your sketch is done! Want to watch it now?”&lt;/p&gt;
&lt;p&gt;This particular example centers on a strategy that uses &amp;ldquo;asynchronous programming” techniques. It&amp;rsquo;s often done to boost app performance and/or responsiveness.&lt;/p&gt;
&lt;p&gt;During that middle part where we&amp;rsquo;re &amp;ldquo;disconnected” from the &lt;code&gt;ImageSketcher&#39;s&lt;/code&gt; &lt;code&gt;sketch()&lt;/code&gt; function, control of the app wouldn&amp;rsquo;t be tied up. Folks could continue to interact with the app.&lt;/p&gt;
&lt;p&gt;From a developer&amp;rsquo;s point of view, he/she can program against the API by calling the function, knowing that at [some unknown point in the future], it will finish, &lt;strong&gt;and that he/she will have the opportunity at that time to &amp;ldquo;hook back in” and respond to that completion event&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;That last bit is critical. Giving other developers the opportunity to re-insert themselves with custom application logic when the asynchronous task ends is very important as an API designer.&lt;/p&gt;
&lt;p&gt;Exactly what you as an API designer communicate back to the caller of your API is up to you, but put yourself in the &lt;em&gt;client&lt;/em&gt; developer&amp;rsquo;s shoes for a moment:&lt;/p&gt;
&lt;p&gt;Wouldn&amp;rsquo;t it be nice to know if something went wrong, or if data (the completed animation, for example) came out of that &lt;code&gt;sketch()&#39;s&lt;/code&gt; work? That&amp;rsquo;s exactly the kind of information that we&amp;rsquo;d expect an API designer would provide us with this completion event.&lt;/p&gt;
&lt;p&gt;So… just what are the options could we give callers of this method to &amp;ldquo;hook-in” and know that the work is done?&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;hook-in-options&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5 id=&#34;hook-in8221-options&#34;&gt;&amp;ldquo;Hook-in” options&lt;/h5&gt;
&lt;p&gt;In scenarios like this, Swift developers have about 3 options to choose from:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Use &lt;a href=&#34;https://www.andrewcbancroft.com/2014/10/08/fundamentals-of-nsnotificationcenter-in-swift/&#34;&gt;NSNotificationCenter&lt;/a&gt; to alert &amp;ldquo;subscribers” that the &lt;code&gt;sketch()&lt;/code&gt; function has completed its job. I wrote about this option in &lt;a href=&#34;https://www.andrewcbancroft.com/2014/10/08/fundamentals-of-nsnotificationcenter-in-swift/&#34;&gt;Fundamentals of NSNotificationCenter in Swift&lt;/a&gt;&lt;/p&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use the &lt;a href=&#34;https://www.andrewcbancroft.com/2015/04/08/how-delegation-works-a-swift-developer-guide/&#34;&gt;delegate pattern&lt;/a&gt; to create a contract between the &lt;code&gt;ImageSketcher&lt;/code&gt; and the caller of the &lt;code&gt;sketch()&lt;/code&gt; function. When &lt;code&gt;sketch()&lt;/code&gt; has completed its task, the appropriate &lt;em&gt;delegate&lt;/em&gt; method will be called. I wrote about this works in &lt;a href=&#34;https://www.andrewcbancroft.com/2015/04/08/how-delegation-works-a-swift-developer-guide/&#34;&gt;How Delegation Works – A Swift Developer’s Guide&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Use a callback&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So callbacks are used as another way for one piece of code to communicate with another piece of code somewhere else in the app.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;how-callbacks-work&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;how-do-callbacks-work&#34;&gt;How do callbacks work?&lt;/h3&gt;
&lt;p&gt;Here is a brief overview of the communication interaction using our hypothetical &lt;code&gt;ImageSketcher&lt;/code&gt; as a working example:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;An API designer has created the &lt;code&gt;sketch(image:completion:)&lt;/code&gt; function, and has chosen to accept a completion &amp;ldquo;callback” as the means of communicating the fact that the animation has been generated and is ready to show the end-user.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Data&lt;/strong&gt;, such as the completed sketch animation instance will be delivered through the completion callback&amp;rsquo;s parameter(s). The &lt;code&gt;completion&lt;/code&gt; parameter of our &lt;code&gt;sketch()&lt;/code&gt; function will have a signature that client developers must adhere to in order to facilitate the delivery of that data.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;A client developer writes up a routine (a function or closure) and passes it as the completion parameter&amp;rsquo;s argument. The function/closure that he/she writes will have a list of parameters that matches up to what the API designer required.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;When &lt;code&gt;sketch()&lt;/code&gt; is finished generating the sketch, the designer of the API has programmed &lt;em&gt;his/her function&lt;/em&gt; to call the callback that &lt;em&gt;you pass in&lt;/em&gt;. The API designer will pass along any data that was generated as arguments to the callback function&amp;rsquo;s parameters.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The client developer&amp;rsquo;s callback logic executes.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Callbacks are functions that often take the form of a &lt;a href=&#34;https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html#//apple_ref/doc/uid/TP40014097-CH11-ID94&#34;&gt;closure&lt;/a&gt; (basically an in-line function with no name that&amp;rsquo;s passed as a parameter to &lt;em&gt;another&lt;/em&gt; function), but they could technically be a named function.&lt;/p&gt;
&lt;p&gt;Perhaps it&amp;rsquo;s easiest to see in code itself. Here&amp;rsquo;s a skeleton view of what that looks like:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// API Designer World&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;SketchAnimation&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// represents some fully-generated animation that&amp;#39;s ready to play by the end user&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ImageSketcher&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 7&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;sketch&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;image&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIImage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;_&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sketchAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SketchAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Void&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// do some crunching to create the SketchAnimation instance...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 9&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;animation&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SketchAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;10&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// invoke the completion callback&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;12&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// pass along the completed sketch animation instance&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;13&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;completion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;animation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;14&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;15&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;16&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;17&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// ---------------------------------------------------------------------&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;18&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;19&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Client Developer World&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;20&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MainViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIViewController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;21&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// ...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;22&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;23&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// end-user interacts with the app somehow to create an image sketch animation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;24&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// when they do, this function is called...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;25&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;createSketchAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;imageToSketch&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIImage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;26&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;sketcher&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ImageSketcher&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;27&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;28&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;sketcher&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sketch&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;image&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;imageToSketch&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;animation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SketchAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Void&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;in&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;29&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;c1&#34;&gt;// This is the callback.  It&amp;#39;s a closure, passed as the argument to the sketch function&amp;#39;s completion parameter&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;30&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;31&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;c1&#34;&gt;// Ask the end-user if they&amp;#39;d like to view the completed animation now...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;32&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;c1&#34;&gt;// You as a develoepr have access to the completed animation through the animation parameter to this closure&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;33&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;p&#34;&gt;})&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;34&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;35&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;36&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// ...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;37&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You&amp;rsquo;ll notice a couple of things…&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;First&lt;/strong&gt;, I&amp;rsquo;ve separated the two &amp;ldquo;worlds” that exist: &amp;ldquo;API Designer World” and &amp;ldquo;Client Developer World”. Hopefully seeing both in action can give you the most complete picture of what&amp;rsquo;s going on with callbacks.&lt;/p&gt;
&lt;p&gt;In &amp;ldquo;API Designer World”, we&amp;rsquo;ve got the &lt;code&gt;ImageSketcher&lt;/code&gt; and its implementation.&lt;/p&gt;
&lt;p&gt;In &amp;ldquo;Client Developer World”, we&amp;rsquo;ve got someone &lt;em&gt;using&lt;/em&gt; an instance of &lt;code&gt;ImageSketcher&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Second&lt;/strong&gt;, notice the interaction. As an API designer, I was thinking, &amp;ldquo;Hey, when my sketching process is complete, I want to let the caller know that it&amp;rsquo;s finished and hand them the completed &lt;code&gt;SketchAnimation&lt;/code&gt; instance. To do that, I&amp;rsquo;ll need them to pass me a function that I can hand it off to via a parameter”.&lt;/p&gt;
&lt;p&gt;As a client developer, I&amp;rsquo;m thinking, &amp;ldquo;Okay, I&amp;rsquo;m going to call &lt;code&gt;sketch()&lt;/code&gt;, but how am I going to know when it&amp;rsquo;s done and how will I get the animation? Oh! I see – I need to give it a completion closure (a callback), and they&amp;rsquo;ll hand me the completed &lt;code&gt;SketchAnimation&lt;/code&gt; instance through my closure&amp;rsquo;s parameter. Sweet!”&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m hoping the &amp;ldquo;thinking out loud” here helps you piece it together.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;examples-ios-sdk&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;examples-from-the-ios-sdk&#34;&gt;Examples from the iOS SDK&lt;/h3&gt;
&lt;p&gt;So how about a few &lt;em&gt;real&lt;/em&gt; examples, say, from the iOS SDK. Where are callbacks used there?&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;uialertcontroller&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;uialertcontroller&#34;&gt;UIAlertController&lt;/h4&gt;
&lt;p&gt;A really simple example of callbacks being used in the wild is when we work with &lt;a href=&#34;https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/&#34;&gt;&lt;code&gt;UIAlertControllers&lt;/code&gt;&lt;/a&gt;. Take a look at this example:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;alertController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;title&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;My Alert&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;message&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;A Message&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;preferredStyle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertControllerStyle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;alert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;OKAction&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertAction&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;title&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;OK&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;style&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertActionStyle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;default&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;handler&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;action&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertAction&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Void&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;in&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Do something based on the user tapping this action button&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Notice that we get an instance of the UIAlertAction that was tapped if we need it&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 7&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;})&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 9&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;alertController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addAction&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OKAction&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;10&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;present&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;alertController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;animated&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;true&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;12&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// We could have provided a completion callback here, too,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;13&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// but we didn&amp;#39;t need to respond to the view controller&amp;#39;s presentation, so we passed nil&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So the &lt;code&gt;UIAlertAction&lt;/code&gt; is actually the thing that takes the callback (the &lt;code&gt;handler&lt;/code&gt; parameter). There&amp;rsquo;s also an example on a View Controller&amp;rsquo;s &lt;code&gt;present()&lt;/code&gt; function. Both are intended to communicate something back to the caller.&lt;/p&gt;
&lt;p&gt;In the case of the &lt;code&gt;UIAlertAction&lt;/code&gt;, the &lt;code&gt;handler&lt;/code&gt; will be the logic to handle the user&amp;rsquo;s tapping on that specific alert button.&lt;/p&gt;
&lt;p&gt;In the case of the &lt;code&gt;present&lt;/code&gt; call, Apple has given us the opportunity to &amp;ldquo;hook in” to the presentation event and know when it&amp;rsquo;s complete, in case we need to perform additional logic at that moment.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;urlsession&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;urlsession&#34;&gt;URLSession&lt;/h4&gt;
&lt;p&gt;The world of HTTP is inherently asynchronous, so you&amp;rsquo;d expect to see some kind of pattern employed to deal with the &amp;ldquo;disconnectedness” of the start and finish of a process, such as making an HTTP request.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;URLSession&lt;/code&gt; encapsulates certain HTTP actions, such as retrieving the contents of a URL, in instances called &lt;code&gt;URLSessionDataTask&lt;/code&gt;. How does it communicate the fact that the HTTP request is complete, along with the data contained in the response? You guessed it: A callback.&lt;/p&gt;
&lt;p&gt;Take a look at this function signature from the &lt;a href=&#34;https://developer.apple.com/reference/foundation/urlsession/1410330-datatask&#34;&gt;Apple Developer Documentation on URLSession&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;dataTask&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;with&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;url&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;URL&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;              &lt;span class=&#34;n&#34;&gt;completionHandler&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;@&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;escaping&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;URLResponse&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Error&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Void&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;URLSessionDataTask&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;code&gt;completionHandler&lt;/code&gt; parameter of this function is the interface that the API designers have created for delivering the resulting payload of the HTTP request when it&amp;rsquo;s finished and ready to hand off for further processing.&lt;/p&gt;
&lt;p&gt;Client developers of this API will be expected to make the call to &lt;code&gt;dataTask(with:completionHandler:)&lt;/code&gt; and supply it a completion callback to know when things are complete.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;animations&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;animations&#34;&gt;Animations&lt;/h4&gt;
&lt;p&gt;You&amp;rsquo;ll see &lt;em&gt;all kinds&lt;/em&gt; of &lt;code&gt;completion&lt;/code&gt; callbacks sprinkled throughout some of the simpler iOS animation APIs.&lt;/p&gt;
&lt;p&gt;If you take a look at the following function signatures from the &lt;a href=&#34;https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/clm/UIView/animateWithDuration:animations:&#34;&gt;Apple Developer Documentation on UIViews&lt;/a&gt;, you&amp;rsquo;ll see the &lt;code&gt;completion&lt;/code&gt; parameters to many of these functions:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;transition&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;with&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;duration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;options&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;animations&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;completion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;transition&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;to&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;duration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;options&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;completion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;animateKeyframes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;withDuration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;delay&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;options&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;animations&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;completion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;addKeyframe&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;withRelativeStartTime&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;relativeDuration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;animations&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;perform&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;_&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;on&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;options&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;animations&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;completion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;animate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;withDuration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;delay&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;usingSpringWithDamping&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;initialSpringVelocity&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;options&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;animations&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;completion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping Up&lt;/h3&gt;
&lt;p&gt;Believe it or not, the usage of callbacks is one of the less-complicated ways of communicating between parts of code.&lt;/p&gt;
&lt;p&gt;My goal in this article was to show all of the sides and perspectives and players to give you insight into how this communication takes place.&lt;/p&gt;
&lt;p&gt;Now that the foundations are laid, it is my hope that you&amp;rsquo;ll be able to more confidently use callbacks and know what&amp;rsquo;s happening as you encounter them in your Swift code!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;related&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&#34;resources&#34;&gt;
  &lt;div class=&#34;resources-header&#34;&gt;
    You might also enjoy&amp;#8230;
  &lt;/div&gt;
  &lt;ul class=&#34;resources-content&#34;&gt;
    &lt;li&gt;
      &lt;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a href=&#34;http://www.andrewcbancroft.com/2015/03/26/what-is-delegation-a-swift-developers-guide/&#34; title=&#34;What is Delegation? – A Swift Developer’s Guide&#34;&gt;What is Delegation? – A Swift Developer’s Guide&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a href=&#34;http://www.andrewcbancroft.com/2015/04/08/how-delegation-works-a-swift-developer-guide/&#34; title=&#34;How Delegation Works – A Swift Developer’s Guide&#34;&gt;How Delegation Works – A Swift Developer’s Guide&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://www.andrewcbancroft.com/2014/10/08/fundamentals-of-nsnotificationcenter-in-swift/&#34; title=&#34;Fundamentals of NSNotificationCenter in Swift&#34;&gt;Fundamentals of NSNotificationCenter in Swift&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;share&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>