{"id":42045,"date":"2024-02-06T13:26:43","date_gmt":"2024-02-06T18:26:43","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=42045"},"modified":"2024-02-06T13:26:43","modified_gmt":"2024-02-06T18:26:43","slug":"swift-tricks","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2024\/02\/06\/swift-tricks\/","title":{"rendered":"Swift Tricks"},"content":{"rendered":"<p><a href=\"https:\/\/eon.codes\/blog\/2024\/01\/27\/Swift-tricks\/\">Andr&eacute; Jorgensen<\/a>:<\/p>\n<blockquote cite=\"https:\/\/eon.codes\/blog\/2024\/01\/27\/Swift-tricks\/\"><p>Generic typealias can be used to simplify param types etc<\/p><pre>typealias Parser&lt;A&gt; = (String) -&gt; [(A, String)]<\/pre>\n<p>[&#8230;]<\/p>\n<pre>func parse&lt;A&gt;(stringToParse: String, parser: Parser)<\/pre>\n<p>[&#8230;]<\/p>\n<p>Finding Elements of Specific Type in Swift<\/p>\n<pre>\nextension Array {\n    func whereType&#60;T&#62;() -&#62; [T] {\n        compactMap { $0 as? T } \/\/ The function \"compactMap(\" in Swift is incredibly useful. It maps each element of an array to another optional type and returns the value if it exists (is not null).\n    }\n}<\/pre>\n<\/blockquote>\n<p>I&rsquo;ve been using a version of this where the type is passed as a parameter. With this version it&rsquo;s determined using type inference from the call site.<\/p>\n<p>He currently has 202 other tricks listed.<\/p>\n\n<p><a href=\"https:\/\/wadetregaskis.com\/downcasting-in-a-for-loop\/\">Wade Tregaskis<\/a>:<\/p>\n<blockquote cite=\"https:\/\/wadetregaskis.com\/downcasting-in-a-for-loop\/\">\n<pre>for case let rep as NSBitmapImageRep in image.representations {\n    &#8230; \/\/ `rep` is an NSBitmapImageRep.  Non-bitmap reps are skipped.\n}<\/pre>\n<p>I sometimes forget that this is possible (and even more often exactly what the damn syntax is &#x2013; kudos to <a href=\"https:\/\/stackoverflow.com\/users\/1630618\/vacawama\">vacawama<\/a> in today&rsquo;s case of this for reminding me with <a href=\"https:\/\/stackoverflow.com\/a\/26076910\/790079\">their StackOverflow answer<\/a>).  There are numerous other ways to write the above, but I think it is the most elegant.<\/p>\n<\/blockquote>\n<p>I&rsquo;ve been writing Swift for almost 10 years and still have to think to remember this syntax, as well as <code>if case let<\/code>. I <em>know<\/em> that <code>case<\/code> is for pattern matching, but it still looks weird to see it there, and this is the same reason it&rsquo;s written using <code>as<\/code> without the question mark that normally accompanies downcasts that might fail. You might expect to be able to write:<\/p>\n<pre>for rep as? NSBitmapImageRep in image.representations {<\/pre>\n<p>but instead Swift gives us <code>case let<\/code> and a more general pattern matching feature. If you write this or get it correct except for the <code>?<\/code>, the compiler&rsquo;s error messages are unhelpful:<\/p>\n\n<pre>for rep as? NSBitmapImageRep in image.representations {\n\/\/ Expected 'in' after for-each pattern\n\nfor case let rep as? NSBitmapImageRep in image.representations {\n\/\/ Pattern variable binding cannot appear in an expression\n<\/pre>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2018\/11\/01\/swifts-if-case-let-syntax\/\">Swift&rsquo;s &ldquo;if case let&rdquo; Syntax<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Andr&eacute; Jorgensen: Generic typealias can be used to simplify param types etctypealias Parser&lt;A&gt; = (String) -&gt; [(A, String)] [&#8230;] func parse&lt;A&gt;(stringToParse: String, parser: Parser) [&#8230;] Finding Elements of Specific Type in Swift extension Array { func whereType&#60;T&#62;() -&#62; [T] { compactMap { $0 as? T } \/\/ The function \"compactMap(\" in Swift is incredibly useful. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2024-02-06T18:26:45Z","apple_news_api_id":"a5ea81e3-7bf1-45d2-a0b4-391a7a720820","apple_news_api_modified_at":"2024-02-06T18:26:45Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/ApeqB43vxRdKgtDkaenIIIA","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[46,71,901],"class_list":["post-42045","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/42045","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=42045"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/42045\/revisions"}],"predecessor-version":[{"id":42046,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/42045\/revisions\/42046"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=42045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=42045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=42045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}