{"id":2550,"date":"2017-01-17T07:14:42","date_gmt":"2017-01-17T15:14:42","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=2550"},"modified":"2017-08-01T16:14:03","modified_gmt":"2017-08-01T23:14:03","slug":"learning-swift","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=2550","title":{"rendered":"Learning Swift"},"content":{"rendered":"<p>Now that Swift 3 is out and the language is stable, I thought I\u2019d write my next app using it. I watched the first few videos of Paul Hegarty\u2019s CS193P class and then started to read <i>The Swift Programming Language (Swift 3)<\/i>. My first inclination, since this is the replacement for Objective C, was to relate the language features of Swift to those of Objective C. Both languages will have to handle strings and arrays so I started there. It didn\u2019t go as well as I thought it would. An immutable string in Swift is initialized as<br \/>\n<pre><code class=\"preserve-code-formatting\">\nlet mySwiftString:String = &quot;This is a string.&quot;<\/code><\/pre><br \/>\nwhereas an immutable string in Objective C is initialized as<br \/>\n<pre><code class=\"preserve-code-formatting\">\nNSString *myObjCString = @[This is a string.];<\/code><\/pre><br \/>\nSo far so good, but the similarity breaks down quickly.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nmySwiftString = &quot;The string now has new content.&quot; \/\/&nbsp;&nbsp;Cannot assign to value: &#039;myString&#039; is a let constant\nmyObjCString = &quot;The string now has new content.&quot; \/\/&nbsp;&nbsp;No problem<\/code><\/pre><\/p>\n<p>Change the &#8216;let&#8217; to &#8216;var&#8217; for the Swift string and it works fine but then you no longer have an immutable string.<\/p>\n<p>In Objective C, &#8216;myObjCString&#8217; is an object created by the String Class. Swift strings are represented by the String type. In Swift, strings, arrays, dictionaries, sets, and numbers are <i>value types<\/i>.<br \/>\nWhen they are passed around, their value is passed around, not a reference to an object. Not so with Objective C where some of these are passed by value and some by reference. And numbers can be either &#8216;primitives&#8217; or NSNumbers.<\/p>\n<p>Another big difference is type safety. Swift is a type-safe language so you must explicitly define the type of everything or else the complier will object. Objective C puts the onus on you to make sure that the type of your object is what the code expects.<\/p>\n<p>I quickly decided that trying to fit Swift into my mental model of Objective C wasn\u2019t going to work. Sure they both handle operations, collection types, control flow, and classes, but their basic approach is so different that trying to draw analogies between the two is counter-productive.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now that Swift 3 is out and the language is stable, I thought I\u2019d write my next app using it. I watched the first few videos of Paul Hegarty\u2019s CS193P class and then started to read The Swift Programming Language (Swift 3). My first inclination, since this is the replacement for Objective C, was to &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=2550\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Learning Swift<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-2550","post","type-post","status-publish","format-standard","hentry","category-ios"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2550","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2550"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2550\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}