{"id":195,"date":"2023-08-17T21:13:00","date_gmt":"2023-08-17T13:13:00","guid":{"rendered":"https:\/\/blog.bexonbai.com\/?p=195"},"modified":"2026-03-02T21:14:18","modified_gmt":"2026-03-02T13:14:18","slug":"closure-in-swift","status":"publish","type":"post","link":"https:\/\/blog.bexonbai.com\/?p=195","title":{"rendered":"Closure in Swift"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Swift\u2019s closure is an anonymous function, think of it as a function that can be declared as a&nbsp;<code>property<\/code>&nbsp;or&nbsp;<code>parameter<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It\u2019s easy to use, just like Objective-C and Kotlin.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>funcName<\/strong>() { (parameter) <strong>-&gt;<\/strong> returnType <strong>in<\/strong>\n    <em>\/\/ Processing logic...<\/em>\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Refer here:&nbsp;<a href=\"https:\/\/note.bexonbai.com\/develop\/2023\/08\/17\/%22https:\/\/fuckingclosuresyntax.com%22\">https:\/\/fuckingclosuresyntax.com<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s a little example:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When calling an API, you will typically do so through a function. Within this function, you will always call the API asynchronously. Afterward, you will need to wait for the asynchronous task to complete. Once it does, the callback will take over and continue the next task.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>func<\/strong> <strong>apiCall<\/strong>(completion: ((<strong>Int<\/strong>)<strong>-&gt;<\/strong><strong>Void<\/strong>)?) {\n  <strong>DispatchQueue<\/strong><strong>.<\/strong>main<strong>.<\/strong><strong>async<\/strong> {\n    <em>\/\/ This optional variable completion: ((Int)-&gt;Void)? is a closure.<\/em>\n    <strong>print<\/strong>(\"Closure Demo\", \"Api calling\")\n    <strong>completion<\/strong>?(2)\n  }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Calling it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>self<\/strong><strong>.<\/strong><strong>apiCall<\/strong>(completion: { value <strong>in<\/strong>\n  <strong>print<\/strong>(\"Closure Demo\", value)\n})\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Or use it as a trailing closure. When your closure is the last parameter, nothing changes, it\u2019s just for code aesthetics.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>self<\/strong><strong>.<\/strong><strong>apiCall<\/strong>() { value <strong>in<\/strong>\n  <strong>print<\/strong>(\"Closure Demo\", value)\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Console print:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Closure Demo Api calling\nClosure Demo 2\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As a higher-order function, closures are frequently used to determine the result of a value. Familiarize yourself with closures as much as possible to make your code neater and more readable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Swift\u2019s closure is an anonymous function, think of it a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-195","post","type-post","status-publish","format-standard","hentry","category-develop"],"_links":{"self":[{"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=\/wp\/v2\/posts\/195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=195"}],"version-history":[{"count":1,"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=\/wp\/v2\/posts\/195\/revisions"}],"predecessor-version":[{"id":196,"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=\/wp\/v2\/posts\/195\/revisions\/196"}],"wp:attachment":[{"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}