{"id":1,"date":"2024-10-09T19:53:59","date_gmt":"2024-10-09T19:53:59","guid":{"rendered":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/?p=1"},"modified":"2024-10-10T06:17:34","modified_gmt":"2024-10-10T06:17:34","slug":"hello-world","status":"publish","type":"post","link":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/2024\/10\/09\/hello-world\/","title":{"rendered":"Important facts of closures in Swift"},"content":{"rendered":"\n<p><ul class=\"\"><br><li id=\"1241\" class=\"ze zf vl zg b zh zi zj zk zl zm zn zo zp zq zr zs zt zu zv zw zx zy zz aba abb abc abd abe bx\" data-selectable-paragraph=\"\">You can write a closure without a name by surrounding code with braces (<code class=\"ct abf abg abh abi b\"><strong class=\"zg gu\">{}<\/strong><\/code>). Use\u00a0<code class=\"ct abf abg abh abi b\"><strong class=\"zg gu\">in<\/strong><\/code>\u00a0in a closure to seperate the arguments and return type from its body.<\/li><\/ul><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    Int numbers = &#91;1,2,3,4,5,6]\n    numbers.map({ (number:Int) -> Int in\n      let result = number * 4\n      return result}\n    )<\/code><\/pre>\n\n\n\n<p><ul class=\"\"><br><li id=\"a506\" class=\"ze zf vl zg b zh zi zj zk zl zm zn zo zp zq zr zs zt zu zv zw zx zy zz aba abb abc abd abe bx\" data-selectable-paragraph=\"\">You can omit the type of its params, its return type or both when a closure\u2019s type is already known.<\/li><br><li id=\"ec11\" class=\"ze zf vl zg b zh aby zj zk zl abz zn zo zp aca zr zs zt acb zv zw zx acc zz aba abb abc abd abe bx\" data-selectable-paragraph=\"\">Single statement closures implicitly return the value of their only statment.<br><br><pre class=\"abj abk abl abm abn abo abi abp abq bn lc bx\"><span id=\"50f9\" class=\"abr abs vl abi b fo abt abu x abv abw\" data-selectable-paragraph=\"\">Int numbers = [1,2,3,4,5,6]<br>numbers.map({number in number*3})<\/span><\/pre><br><\/li><br><li id=\"d057\" class=\"ze zf vl zg b zh zi zj zk zl zm zn zo zp zq zr zs zt zu zv zw zx zy zz aba abb abc abd abe bx\" data-selectable-paragraph=\"\">You can refer to params by numbers instead of names. The\u00a0<code class=\"ct abf abg abh abi b\">$0<\/code>\u00a0is the first input parameter for the closure. If the closure had multiple parameters, the second parameter would be\u00a0<code class=\"ct abf abg abh abi b\">$1<\/code>, the third\u00a0<code class=\"ct abf abg abh abi b\">$2<\/code>, etcetera.<\/li><br><\/ul><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><p>\u00a0numbers.map({$0*3})<\/p><\/code><\/pre>\n\n\n\n<p><br><br><ul><br><li>When a closure is the only argument to a function, you can omit the parentheses entirely.<\/li><br><\/ul><br><pre class=\"abj abk abl abm abn abo abi abp abq bn lc bx\"><span id=\"7380\" class=\"abr abs vl abi b fo abt abu x abv abw\" data-selectable-paragraph=\"\">numbers.map{$0*3}numbers.sorted{$0 > $1}<br><\/span><\/pre><br><ul class=\"\"><br><li id=\"d33e\" class=\"ze zf vl zg b zh zi zj zk zl zm zn zo zp zq zr zs zt zu zv zw zx zy zz aba abb abc abd abe bx\" data-selectable-paragraph=\"\">A closure passed as the last argument to a function can appear immediately after the paranthesis.<\/li><br><\/ul><br><pre class=\"abj abk abl abm abn abo abi abp abq bn lc bx\">numbers.reduce(0){$0+$1}<\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can write a closure without a name by surrounding code with braces ({}). Use\u00a0in\u00a0in a closure to seperate the arguments and return type from&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1","post","type-post","status-publish","format-standard","hentry","category-basic-swift"],"menu_order":0,"_links":{"self":[{"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/posts\/1","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/comments?post=1"}],"version-history":[{"count":6,"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/posts\/1\/revisions"}],"predecessor-version":[{"id":13,"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/posts\/1\/revisions\/13"}],"wp:attachment":[{"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/media?parent=1"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/categories?post=1"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/tags?post=1"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}