{"id":1010,"date":"2018-06-25T20:40:04","date_gmt":"2018-06-25T18:40:04","guid":{"rendered":"http:\/\/www.albertopasca.it\/whiletrue\/?p=1010"},"modified":"2018-06-19T17:14:20","modified_gmt":"2018-06-19T15:14:20","slug":"swift-use-generics-with-closures","status":"publish","type":"post","link":"https:\/\/www.albertopasca.it\/whiletrue\/swift-use-generics-with-closures\/","title":{"rendered":"Swift &#8211; Use generics [T] with closures"},"content":{"rendered":"<p>Let&#8217;s make an example to understand better the scope of this tutorial.<\/p>\n<p>You want to:<\/p>\n<ul>\n<li><strong>increment<\/strong> all numbers in array\u00a0by 1<\/li>\n<li><strong>double<\/strong> all numbers in array<\/li>\n<li>check if the numbers in array are <strong>even<\/strong> or <strong>odd<\/strong><\/li>\n<li><strong>multiply<\/strong> all numbers in array<\/li>\n<li>more and more&#8230;<\/li>\n<\/ul>\n<p>How many functions should you create to do this?<\/p>\n<p><!--more--><\/p>\n<h5>ONE\u00a0<em>using generics<\/em>, of course, <em>4 without<\/em> generics.<\/h5>\n<p>But what are generics? (<a href=\"https:\/\/docs.swift.org\/swift-book\/LanguageGuide\/Generics.html\" target=\"_blank\" rel=\"noopener\">reference<\/a>)<\/p>\n<blockquote><p><em>Generic code<\/em>\u00a0enables you to write flexible, reusable functions and types that can work with any type, subject to requirements that you define. You can write code that avoids duplication and expresses its intent in a clear, abstracted manner.<\/p>\n<p>Generics are one of the most powerful features of Swift, and much of the Swift standard library is built with generic code. In fact, you\u2019ve been using generics throughout the\u00a0<em>Language Guide<\/em>, even if you didn\u2019t realize it. For example, Swift\u2019s\u00a0<code class=\"docutils literal\"><span class=\"pre\">Array<\/span><\/code>\u00a0and\u00a0<code class=\"docutils literal\"><span class=\"pre\">Dictionary<\/span><\/code>types are both generic collections. You can create an array that holds\u00a0<code class=\"docutils literal\"><span class=\"pre\">Int<\/span><\/code>\u00a0values, or an array that holds\u00a0<code class=\"docutils literal\"><span class=\"pre\">String<\/span><\/code>\u00a0values, or indeed an array for any other type that can be created in Swift. Similarly, you can create a dictionary to store values of any specified type, and there are no limitations on what that type can be.<\/p><\/blockquote>\n<hr \/>\n<p>Let&#8217;s put some verbose code:<\/p>\n<h6>Increment an array element by 1<\/h6>\n<pre class=\"p1\"><span class=\"s1\">func<\/span> increment(array: [<span class=\"s2\">Int<\/span>]) -&gt; [<span class=\"s2\">Int<\/span>] {\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s1\">var<\/span> result: [<span class=\"s2\">Int<\/span>] = []\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s1\">for<\/span> x <span class=\"s1\">in<\/span> array {\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span>result.<span class=\"s3\">append<\/span>(x + <span class=\"s4\">1<\/span>)\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>}\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s1\">return<\/span> result\r\n}<\/pre>\n<h6>Multiply an array element by 2<\/h6>\n<pre class=\"p1\"><span class=\"s1\">func<\/span> double(array: [<span class=\"s2\">Int<\/span>]) -&gt; [<span class=\"s2\">Int<\/span>] {\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s1\">var<\/span> result: [<span class=\"s2\">Int<\/span>] = []\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s1\">for<\/span> x <span class=\"s1\">in<\/span> array {\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span>result.<span class=\"s3\">append<\/span>(x * <span class=\"s4\">2<\/span>)\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>}\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s1\">return<\/span> result\r\n}<\/pre>\n<h6>Multiply an array element by ITSELF<\/h6>\n<pre class=\"p1\"><span class=\"s1\">func<\/span> multiply(array: [<span class=\"s2\">Int<\/span>]) -&gt; [<span class=\"s2\">Int<\/span>] {\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s1\">var<\/span> result: [<span class=\"s2\">Int<\/span>] = []\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s1\">for<\/span> x <span class=\"s1\">in<\/span> array {\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span>result.<span class=\"s3\">append<\/span>(x * x)\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>}\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s1\">return<\/span> result\r\n}<\/pre>\n<h6>Check is an element of array is even\/odd<\/h6>\n<pre class=\"p1\"><span class=\"s1\">func<\/span> isItemEven(array: [<span class=\"s2\">Int<\/span>]) -&gt; [<span class=\"s2\">Bool<\/span>] {\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s1\">var<\/span> result: [<span class=\"s2\">Bool<\/span>] = []\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s1\">for<\/span> x <span class=\"s1\">in<\/span> array {\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span>result.<span class=\"s3\">append<\/span>(x % <span class=\"s4\">2<\/span> == <span class=\"s4\">0<\/span>)\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>}\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s1\">return<\/span> result\r\n}<\/pre>\n<p>So this is the point. You basically need 4 different functions to play with this INT array. Most of the code is duplicated, except for the formula.<\/p>\n<pre class=\"p1\"><span class=\"s1\">print<\/span>( <span class=\"s1\">increment<\/span>(array: [<span class=\"s2\">1<\/span>,<span class=\"s2\">2<\/span>,<span class=\"s2\">3<\/span>,<span class=\"s2\">4<\/span>,<span class=\"s2\">5<\/span>]) )\r\n<span class=\"s1\">[2, 3, 4, 5, 6]<\/span>\r\n\r\n<span class=\"s1\">print<\/span>( <span class=\"s1\">double<\/span>(array: [<span class=\"s2\">1<\/span>,<span class=\"s2\">2<\/span>,<span class=\"s2\">3<\/span>,<span class=\"s2\">4<\/span>,<span class=\"s2\">5<\/span>]) )\r\n<span class=\"s1\">[2, 4, 6, 8, 10]<\/span>\r\n\r\n<span class=\"s1\">print<\/span>( <span class=\"s1\">multiply<\/span>(array: [<span class=\"s2\">1<\/span>,<span class=\"s2\">2<\/span>,<span class=\"s2\">3<\/span>,<span class=\"s2\">4<\/span>,<span class=\"s2\">5<\/span>]) )\r\n<span class=\"s1\">[1, 4, 9, 16, 25]\r\n<\/span>\r\n<span class=\"s1\">print<\/span>( <span class=\"s1\">isItemEven<\/span>(array: [<span class=\"s2\">1<\/span>,<span class=\"s2\">2<\/span>,<span class=\"s2\">3<\/span>,<span class=\"s2\">4<\/span>,<span class=\"s2\">5<\/span>]) )\r\n<span class=\"s1\">[false, true, false, true, false]<\/span><\/pre>\n<p class=\"p1\">How can we improve these methods using generics?<\/p>\n<p>We can create a method called &#8220;<strong>compute&lt;T&gt;<\/strong>&#8221; that do the job:<\/p>\n<pre class=\"p1\"><span class=\"s1\">func<\/span> <strong>compute<\/strong>&lt;T&gt;( array: [<span class=\"s2\">Int<\/span>], transform: (<span class=\"s2\">Int<\/span>) -&gt; <span class=\"s2\">T<\/span>) -&gt; [<span class=\"s2\">T<\/span>] {\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s1\">var<\/span> result: [<span class=\"s2\">T<\/span>] = []\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s1\">for<\/span> x <span class=\"s1\">in<\/span> array {\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span>result.<span class=\"s3\">append<\/span>(transform(x))\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>}\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s1\">return<\/span> result\r\n}<\/pre>\n<p>This method accepts as input parameters an <strong>array of INT<\/strong> (what we want)\u00a0and a <strong>closure <\/strong>(that make the magic). Returns an <strong>array of [T]<\/strong>, <em>generic type<\/em>.<\/p>\n<p>How to use?<\/p>\n<p>We can create a simple method (just to make stuff ordered&#8230;) that increment numbers calling our new<strong>compute<\/strong> function passing the formula:<\/p>\n<pre class=\"p1\"><span class=\"s1\">func<\/span> incrementNumbers(array: [<span class=\"s2\">Int<\/span>]) -&gt; [<span class=\"s2\">Int<\/span>] {\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s1\">return<\/span> <strong>compute<\/strong>(array: array, transform: { $0 + <span class=\"s4\">1<\/span> })\r\n}<\/pre>\n<p>This method add 1 to the <strong>$0<\/strong> (that is the current element of array) and return the incremented value.<\/p>\n<p>What about others? Same thing as before:<\/p>\n<pre class=\"p1\"><span class=\"s1\">func<\/span> multiply(array: [<span class=\"s2\">Int<\/span>]) -&gt; [<span class=\"s2\">Int<\/span>] {\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s1\">return<\/span> <strong>compute<\/strong>(array: array, transform: { $0 * <span class=\"s4\">$0<\/span> })\r\n}<\/pre>\n<p>The cool thing is that\u00a0 you can use the same function also for boolean for instance:<\/p>\n<pre class=\"p1\"><span class=\"s1\">func<\/span> <span class=\"s1\">isItemEven<\/span>(array: [<span class=\"s2\">Int<\/span>]) -&gt; [<span class=\"s2\">Bool<\/span>] {\r\n<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s1\">return<\/span> <strong>compute<\/strong>(array: array, transform: { $0 % <span class=\"s4\">2<\/span> == <span class=\"s4\">0<\/span> })\r\n}<\/pre>\n<p>without changing anything and complicate your life!<\/p>\n<p>Cool right?<\/p>\n<p>Test it in playground:<\/p>\n<pre><span class=\"s1\">print<\/span>( <strong>compute<\/strong>(array: [<span class=\"s2\">1<\/span>,<span class=\"s2\">2<\/span>,<span class=\"s2\">3<\/span>,<span class=\"s2\">4<\/span>,<span class=\"s2\">5<\/span>], transform: { $0 + <span class=\"s2\">1<\/span> }) )\r\n<span class=\"s1\">[2, 3, 4, 5, 6]<\/span>\r\n\r\n<span class=\"s1\">print<\/span>( <strong>compute<\/strong>(array: [<span class=\"s2\">1<\/span>,<span class=\"s2\">2<\/span>,<span class=\"s2\">3<\/span>,<span class=\"s2\">4<\/span>,<span class=\"s2\">5<\/span>], transform: { $0 * <span class=\"s2\">2<\/span> }) )\r\n<span class=\"s1\">[2, 4, 6, 8, 10]<\/span>\r\n\r\n<span class=\"s1\">print<\/span>( <strong>compute<\/strong>(array: [<span class=\"s2\">1<\/span>,<span class=\"s2\">2<\/span>,<span class=\"s2\">3<\/span>,<span class=\"s2\">4<\/span>,<span class=\"s2\">5<\/span>], transform: { $0 * $0 }) )\r\n<span class=\"s1\">[1, 4, 9, 16, 25]<\/span>\r\n\r\n<span class=\"s1\">print<\/span>( <strong>compute<\/strong>(array: [<span class=\"s2\">1<\/span>,<span class=\"s2\">2<\/span>,<span class=\"s2\">3<\/span>,<span class=\"s2\">4<\/span>,<span class=\"s2\">5<\/span>], transform: { $0 % <span class=\"s2\">2<\/span> == <span class=\"s2\">0<\/span> }) )\r\n<span class=\"s1\">[false, true, false, true, false]<\/span><\/pre>\n<p>You can customize what you want, this is just an example.<\/p>\n<p>&nbsp;<\/p>\n<p>thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s make an example to understand better the scope of this tutorial. You want to: increment all numbers in array\u00a0by 1 double all numbers in array check if the numbers in array are even or odd multiply all numbers in array more and more&#8230; How many functions should you create to do this?<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1],"tags":[649,654,652,653,650,648,655,46,651],"class_list":["post-1010","post","type-post","status-publish","format-standard","hentry","category-generic","tag-array","tag-bool","tag-closure","tag-compute","tag-function","tag-generics","tag-int","tag-swift","tag-transform"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Swift - Use generics [T] with closures - while (true) {\u00a0}<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.albertopasca.it\/whiletrue\/swift-use-generics-with-closures\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Swift - Use generics [T] with closures - while (true) {\u00a0}\" \/>\n<meta property=\"og:description\" content=\"Let&#8217;s make an example to understand better the scope of this tutorial. You want to: increment all numbers in array\u00a0by 1 double all numbers in array check if the numbers in array are even or odd multiply all numbers in array more and more&#8230; How many functions should you create to do this?\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.albertopasca.it\/whiletrue\/swift-use-generics-with-closures\/\" \/>\n<meta property=\"og:site_name\" content=\"while (true) {\u00a0}\" \/>\n<meta property=\"article:published_time\" content=\"2018-06-25T18:40:04+00:00\" \/>\n<meta name=\"author\" content=\"Alberto Pasca\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@lupasca\" \/>\n<meta name=\"twitter:site\" content=\"@lupasca\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alberto Pasca\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-use-generics-with-closures\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-use-generics-with-closures\\\/\"},\"author\":{\"name\":\"Alberto Pasca\",\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\"},\"headline\":\"Swift &#8211; Use generics [T] with closures\",\"datePublished\":\"2018-06-25T18:40:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-use-generics-with-closures\\\/\"},\"wordCount\":411,\"publisher\":{\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\"},\"keywords\":[\"array\",\"bool\",\"closure\",\"compute\",\"function\",\"generics\",\"int\",\"swift\",\"transform\"],\"articleSection\":[\"generic\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-use-generics-with-closures\\\/\",\"url\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-use-generics-with-closures\\\/\",\"name\":\"Swift - Use generics [T] with closures - while (true) {\u00a0}\",\"isPartOf\":{\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#website\"},\"datePublished\":\"2018-06-25T18:40:04+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-use-generics-with-closures\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-use-generics-with-closures\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-use-generics-with-closures\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Swift &#8211; Use generics [T] with closures\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#website\",\"url\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/\",\"name\":\"while (true) {\u00a0}\",\"description\":\"notes from a developer, tutorials, how-to, ideas\",\"publisher\":{\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\",\"name\":\"Alberto Pasca\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\",\"caption\":\"Alberto Pasca\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\"},\"description\":\"Software engineer @ Pirelli &amp; C. S.p.A. with a strong passion for mobile \uf8ff development, security, and connected things.\",\"sameAs\":[\"https:\\\/\\\/www.albertopasca.it\",\"albertopasca\",\"https:\\\/\\\/x.com\\\/lupasca\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Swift - Use generics [T] with closures - while (true) {\u00a0}","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.albertopasca.it\/whiletrue\/swift-use-generics-with-closures\/","og_locale":"en_US","og_type":"article","og_title":"Swift - Use generics [T] with closures - while (true) {\u00a0}","og_description":"Let&#8217;s make an example to understand better the scope of this tutorial. You want to: increment all numbers in array\u00a0by 1 double all numbers in array check if the numbers in array are even or odd multiply all numbers in array more and more&#8230; How many functions should you create to do this?","og_url":"https:\/\/www.albertopasca.it\/whiletrue\/swift-use-generics-with-closures\/","og_site_name":"while (true) {\u00a0}","article_published_time":"2018-06-25T18:40:04+00:00","author":"Alberto Pasca","twitter_card":"summary_large_image","twitter_creator":"@lupasca","twitter_site":"@lupasca","twitter_misc":{"Written by":"Alberto Pasca","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.albertopasca.it\/whiletrue\/swift-use-generics-with-closures\/#article","isPartOf":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/swift-use-generics-with-closures\/"},"author":{"name":"Alberto Pasca","@id":"http:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac"},"headline":"Swift &#8211; Use generics [T] with closures","datePublished":"2018-06-25T18:40:04+00:00","mainEntityOfPage":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/swift-use-generics-with-closures\/"},"wordCount":411,"publisher":{"@id":"http:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac"},"keywords":["array","bool","closure","compute","function","generics","int","swift","transform"],"articleSection":["generic"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.albertopasca.it\/whiletrue\/swift-use-generics-with-closures\/","url":"https:\/\/www.albertopasca.it\/whiletrue\/swift-use-generics-with-closures\/","name":"Swift - Use generics [T] with closures - while (true) {\u00a0}","isPartOf":{"@id":"http:\/\/www.albertopasca.it\/whiletrue\/#website"},"datePublished":"2018-06-25T18:40:04+00:00","breadcrumb":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/swift-use-generics-with-closures\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.albertopasca.it\/whiletrue\/swift-use-generics-with-closures\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.albertopasca.it\/whiletrue\/swift-use-generics-with-closures\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/www.albertopasca.it\/whiletrue\/"},{"@type":"ListItem","position":2,"name":"Swift &#8211; Use generics [T] with closures"}]},{"@type":"WebSite","@id":"http:\/\/www.albertopasca.it\/whiletrue\/#website","url":"http:\/\/www.albertopasca.it\/whiletrue\/","name":"while (true) {\u00a0}","description":"notes from a developer, tutorials, how-to, ideas","publisher":{"@id":"http:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.albertopasca.it\/whiletrue\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"http:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac","name":"Alberto Pasca","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g","caption":"Alberto Pasca"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g"},"description":"Software engineer @ Pirelli &amp; C. S.p.A. with a strong passion for mobile \uf8ff development, security, and connected things.","sameAs":["https:\/\/www.albertopasca.it","albertopasca","https:\/\/x.com\/lupasca"]}]}},"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts\/1010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/comments?post=1010"}],"version-history":[{"count":8,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts\/1010\/revisions"}],"predecessor-version":[{"id":1018,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts\/1010\/revisions\/1018"}],"wp:attachment":[{"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/media?parent=1010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/categories?post=1010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/tags?post=1010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}