{"id":2813,"date":"2016-04-26T09:21:02","date_gmt":"2016-04-26T15:21:02","guid":{"rendered":"http:\/\/dev.iachieved.it\/iachievedit\/?p=2813"},"modified":"2016-09-20T21:33:49","modified_gmt":"2016-09-21T02:33:49","slug":"moving-to-swift-3-0","status":"publish","type":"post","link":"https:\/\/dev.iachieved.it\/iachievedit\/moving-to-swift-3-0\/","title":{"rendered":"Moving to Swift 3.0"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.shields.io\/badge\/Swift-3.0-orange.svg?style=flat\" alt=\"Swift 3.0\" \/><\/p>\n<p>Hey!  Interested in an <code>apt-get<\/code> repository for Swift 3.0?  We&#8217;ve got packages for <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/introducing-swift-3-0\/\">Xenial, Wily, and Trusty<\/a>.<\/p>\n<p>Swift 3.0 is introducing a number of changes, the least of which is moving to conformance of the ratified <a href=\"https:\/\/swift.org\/documentation\/api-design-guidelines\/\">API naming conventions<\/a>.  If you read through the naming conventions you&#8217;ll see that the overarching theme is one of streamlining and simplification.<\/p>\n<p>Let&#8217;s take a look at a few examples of this streamlining with this 2.2 script <code>swiftcat.swift<\/code>:<\/p>\n<pre class=\"lang:swift\">\nimport Glibc\n\nguard Process.arguments.count == 2 else {\n  print(\"Usage:  swiftcat FILENAME\")\n  exit(-1)\n}\n\nlet filename = Process.arguments[1]\n\nlet BUFSIZE = 1024\nvar pp      = popen(\"cat \" + filename, \"r\")\nvar buf     = [CChar](count:BUFSIZE, repeatedValue:CChar(0))\n\nwhile fgets(&buf, Int32(BUFSIZE), pp) != nil {\n  print(String.fromCString(buf)!, terminator:\"\")\n}\n\nexit(0)\n<\/pre>\n<p>Swift 3.0 changes the <code>(count:repeatedValue)<\/code> function signature for array initialization to <code>(repeating:count:)<\/code>.  So rather than <code>var buf     = [CChar](count:BUFSIZE, repeatedValue:CChar(0))<\/code> we will use <code>var buf = [CChar](repeating:0, count:BUFSIZE)<\/code> (note that we also simplified <code>CChar(0)<\/code> to just <code>0<\/code>).<\/p>\n<p>The <code>fromCString<\/code> <code>String<\/code> method has been removed in favor of an initializer in the form of <code>init(cString:)<\/code>.  As an initializer it also guarantees an object will be returned, so there&#8217;s no need for using the <code>!<\/code>:  <code>print (String(cString:buf), terminator:\"\")<\/code><\/p>\n<p>And <i>finally<\/i>, <code>CommandLine<\/code> is used instead of <code>Process<\/code>.  Our new Swift 3.0 version of <code>swiftcat.swift<\/code>:<\/p>\n<pre>\nimport Glibc\n\nguard CommandLine.arguments.count == 2 else {\n  print(\"Usage:  swiftcat FILENAME\")\n  exit(-1)\n}\n\nlet filename = CommandLine.arguments[1]\n\nlet BUFSIZE = 1024\nvar pp      = popen(\"cat \" + filename, \"r\")\nvar buf     = [CChar](repeating:0, count:BUFSIZE)\n\nwhile fgets(&buf, Int32(BUFSIZE), pp) != nil {\n  print(String(cString:buf), terminator:\"\")\n}\n\nexit(0)\n<\/pre>\n<p>Let&#8217;s take a look at another change in Swift 3.0 (which I appreciate), and that&#8217;s the <i>named parameter<\/i> is now required for the first argument to a function.  For example, in Swift 2.2 the function <code>func translate(text:String, from:String, to:String)<\/code> would be invoked with something like <code>translate(\"Hello world\", from:\"en\", to:\"es\")<\/code>.  In Swift 3.0 the <code>text<\/code> parameter label is required:  <code>translate(text:\"Hello world\", from:\"en\", to:\"es\")<\/code>.<\/p>\n<p>A few more examples of changes made:<\/p>\n<p>Regardless of what you think about the wisdom of this <code>String<\/code> extension, this is functional Swift 2.2 code:<\/p>\n<pre class=\"lang:swift\">\nextension String {\n  subscript (r: Range<Int>) -> String {\n    get {\n      let startIndex = self.startIndex.advancedBy(r.startIndex)\n      let endIndex   = self.startIndex.advancedBy(r.endIndex)\n      return self[Range(start: startIndex, end: endIndex)]\n    }\n  }\n}\n<\/pre>\n<p>The Swift 3.0 complains that <code>value of type 'Index' (aka 'String.CharacterView.Index') has no member 'advancedBy'<\/code>.  That&#8217;s because the function has changed from <code>advancedBy(_:Index)<\/code> to <code>advanced(by:Index)<\/code> (and somewhere buried in the API guidelines is a rationale as to the change).<\/p>\n<p>Our next error is <code>init(start:end:) is unavailable: use the '..&lt;' operator<\/code>.  I must admit that the <code>..&lt;<\/code> operator looks <i>weird<\/i> (there, I said it), but okay, we&#8217;ll play along and update the code to <code>return self[Range(startIndex..&lt;endIndex)]<\/code>.<\/p>\n<h3>In-N-Out (Burger)<\/h3>\n<p>The placement of the <code>inout<\/code> keyword has also changed.  In Swift 2.2 our function for emulating certain <code>ncurses<\/code> calls looked like this:<\/p>\n<pre class=\"lang:swift\">\nfunc getmaxyx(window:UnsafeMutablePointer<WINDOW>, inout y:Int32, inout x:Int32) {\n   x = getmaxx(window)\n   y = getmaxy(window)\n }\n<\/pre>\n<p>That resulted in <code>error: 'inout' before a parameter name is not allowed, place it before the parameter type instead<\/code>, so now we do this:<\/p>\n<pre class=\"lang:swift\">\nfunc getmaxyx(window:UnsafeMutablePointer<WINDOW>, y:inout Int32, x:inout Int32) {\n   x = getmaxx(window)\n   y = getmaxy(window)\n }\n<\/pre>\n<h3>Great Renaming<\/h3>\n<p>As a part of the <i>Great Renaming<\/i> the <code>NS<\/code> prefix has been dropped from much of the Foundation classes.  <code>NSThread<\/code> is now <code>Thread<\/code>, <code>NSDate<\/code> is now <code>Date<\/code>.  You get the idea.<\/p>\n<p>In addition to renaming classes, there has been a significant number of API changes centered around <i>simplifying<\/i> things.  A great example of this simplification can be found in formatting dates and creating dates from strings.  See our post on <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/handling-dates-with-swift-3-0\/\">handling dates<\/a> for more examples.<\/p>\n<h3>Notifications<\/h3>\n<p>Using <code>NSNotificationCenter<\/code> has also changed between Swift 2.x and Swift 3.0.  As a part of the Great Renaming the notification center is now simply named <code>NotificationCenter<\/code>.  For a complete writeup on notifications see our <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/notifications-and-userinfo-with-swift-3-0\/\">Notifications and userinfo with Swift 3.0<\/a> post.<\/p>\n<h3>But Wait!<\/h3>\n<p>Is that all there is to Swift 3.0 you ask?  Of course not!  The <a href=\"https:\/\/github.com\/apple\/swift-evolution\">Swift Evolution<\/a> repository has a complete list of all of the various changes.  Have a look!<\/p>\n<h2>Get Started With Swift 3.0<\/h2>\n<p>There are a number of ways to start playing with Swift 3.0 today.  Apple releases snapshot builds every so often on the official <a href=\"https:\/\/swift.org\/download\/\">Download Swift<\/a> page.  Make sure and grab one of the <a href=\"https:\/\/swift.org\/download\/#snapshots\">Trunk Development (master)<\/a> snapshots.  The snapshots are in the form of <code>.tar.gz<\/code> files that can be extracted on your filesystem.<\/p>\n<p>If you&#8217;re running Ubuntu 14.04 or Ubuntu 15.10 and like the convenience of installing software with <code>apt-get<\/code>, you can also try out our <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/introducing-swift-3-0\/\">Swift 3.0 Ubuntu Packages<\/a>.<\/p>\n<p>Finally, get some practice.  There&#8217;s some code in our <a href=\"https:\/\/github.com\/iachievedit\/moreswift\">moreswift<\/a> Github repository that is all written in Swift 2.2.  As an exercise download this code and try running with a Swift 3.0 compiler.  There&#8217;s no telling what you&#8217;ll run into; if you find something that isn&#8217;t addressed here drop us a line so we can update the post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey! Interested in an apt-get repository for Swift 3.0? We&#8217;ve got packages for Xenial, Wily, and Trusty. Swift 3.0 is introducing a number of changes, the least of which is moving to conformance of the ratified API naming conventions. If you read through the naming conventions you&#8217;ll see that the overarching theme is one of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2846,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-2813","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/2813"}],"collection":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/comments?post=2813"}],"version-history":[{"count":36,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/2813\/revisions"}],"predecessor-version":[{"id":3236,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/2813\/revisions\/3236"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media\/2846"}],"wp:attachment":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media?parent=2813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/categories?post=2813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/tags?post=2813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}