{"id":2874,"date":"2014-07-20T22:53:37","date_gmt":"2014-07-21T01:53:37","guid":{"rendered":"http:\/\/brockerhoff.net\/blog\/?p=2874"},"modified":"2014-07-26T12:34:29","modified_gmt":"2014-07-26T15:34:29","slug":"swift-a-learning-experience","status":"publish","type":"post","link":"https:\/\/brockerhoff.net\/blog\/2014\/07\/20\/swift-a-learning-experience\/","title":{"rendered":"Swift: a learning experience"},"content":{"rendered":"<p>A little over a month has passed since <a href=\"https:\/\/developer.apple.com\/swift\/\" target=\"_blank\">Swift<\/a> came out, and I&#8217;ve just pushed <a href=\"https:\/\/github.com\/rbrockerhoff\/SwiftChecker\" target=\"_blank\">a substantial update<\/a> of my <a href=\"\/blog\/2014\/07\/04\/swiftchecker-a-simple-swift-app\/\" target=\"_blank\">SwiftChecker app<\/a> to GitHub. (<strong>Update:<\/strong> while writing this, I discovered a bug and pushed an <em>updated<\/em> update!)<\/p>\n<p>While I still have to go back now and then to delete semicolons and to correct \u201cString s\u201d to \u201clet s: String\u201d (!@#$% muscle memory!) I finally can write out dozens of lines of Swift code without going back to the documentation or, worse, spend hours on the Internet checking why the compiler is balking. I suppose that means I&#8217;m assimilating the new syntax. It&#8217;s been over a decade since I had to learn a new language, so this isn&#8217;t too bad. \ud83d\ude42<\/p>\n<p>Looking at my new code, I&#8217;m particularly impressed by the conciseness of the language \u2014 the\u00a0interaction with Cocoa APIs is still quite verbose, of course, but once I got used to type inference, the various map\/filter\/reduce\/join functions, and of course generics,\u00a0type extensions and operator declarations, I saw new ways of refactoring my code to be both more concise <em>and<\/em> more understandable.<\/p>\n<p>In my almost 14 years of writing ObjC code I experimented with adding categories\u00a0to existing classes but for the most part it always felt fragile and, after the initial experimental period, I went back\u00a0writing subclasses or container classes. Using C macros to simplify stuff was tricky and error-prone. In Swift, by using generic types, it&#8217;s possible to write extensions and operators such that they don&#8217;t\u00a0conflict with the existing implementation and this feels much more safe and natural. Although, of course, you still can unintentionally conflict with future additions to the Swift Library, namespaces should mostly take care of that.<\/p>\n<p>Generics and type safety are a great help once I got used to them, which admittedly took some futzing around \u2014 the compiler is still very sensitive and the error messages are often cryptic or downright wrong. The type constraints and matching used for more complicated\u00a0generics, admittedly, can become very complex \u2014 especially to someone not current on this new-fangled \u201ctype theory\u201d. Still I\u00a0was able to understand and build some simple generic extensions and functions that could be safely factored out into a few lines and greatly simplify other parts of my code.<\/p>\n<p>Regarding optionals, I quickly got used to them; the old\u00a0\u201creturning nil\u201d dance is now much less error-prone and at every step it&#8217;s clear which type is being handled and when it can be nil or not. The code is more readable in that regard, even if some\u00a0nested \u201cif let\u201d statements have to remain when handling Cocoa return values. And ARC is practically transparent in Swift.<\/p>\n<p>Speaking of values and ARC, the most troublesome parts are still those APIs \u2014\u00a0like the Security framework APIs I use in SwiftChecker \u2014 which haven&#8217;t yet been properly annotated. Certainly\u00a0this will take many months, but no doubt pending updates to\u00a0Clang will also take advantage of that, so that we&#8217;ll finally get rid of those pesky bridging casts.<\/p>\n<p>One other positive side-effect is that the new whitespace rules, while mostly aligned with my own preferences, are finally making me insert spaces after commas, colons and around infix operators, and take other measures for increased readability.<\/p>\n<p>All in all I&#8217;m very optimistic about Swift&#8217;s future. Can&#8217;t wait for the next Xcode beta to come out; the rumor mill says tomorrow, so let&#8217;s hope!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A little over a month has passed since Swift came out, and I&#8217;ve just pushed a substantial update of my SwiftChecker app to GitHub. (Update: while writing this, I discovered a bug and pushed an updated update!) While I still have to go back now and then to delete semicolons and to correct \u201cString s\u201d [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,4,19],"tags":[26,47],"class_list":["post-2874","post","type-post","status-publish","format-standard","hentry","category-apple","category-dev","category-software","tag-cocoa","tag-swift"],"featured_image_src":null,"author_info":{"display_name":"Rainer Brockerhoff","author_link":"https:\/\/brockerhoff.net\/blog\/author\/rbrockerhoff\/"},"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1q3Zc-Km","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/posts\/2874","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/comments?post=2874"}],"version-history":[{"count":0,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/posts\/2874\/revisions"}],"wp:attachment":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/media?parent=2874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/categories?post=2874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/tags?post=2874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}