{"id":1839,"date":"2015-06-10T10:07:14","date_gmt":"2015-06-10T16:07:14","guid":{"rendered":"http:\/\/jamesonquave.com\/blog\/?p=1839"},"modified":"2015-06-11T10:00:47","modified_gmt":"2015-06-11T16:00:47","slug":"swift-2-whats-new","status":"publish","type":"post","link":"https:\/\/jamesonquave.com\/blog\/swift-2-whats-new\/","title":{"rendered":"Swift 2 &#8211; What&#8217;s new"},"content":{"rendered":"<p>Monday Apple announced Swift 2, and just about everything announced is an extremely welcome change. This post is a summary of Chris Lattner&#8217;s discussion in the WWDC video <a href=\"https:\/\/developer.apple.com\/videos\/wwdc\/2015\/?id=106\">&#8220;What&#8217;s new in Swift&#8221;<\/a>. Now, let&#8217;s run through them&#8230;<\/p>\n<p><strong>Fundamentals<\/strong><\/p>\n<p><strong>enums<\/strong> can now be printed to the console and show the actually value instead of just (enum value). Additionally, <strong>println<\/strong> is now just <strong>print<\/strong>. If you have ever used log debugging to figure out a problem involving enum values, rejoice!<\/p>\n<p>I tested this and saw that not only is the enum value is printed, but so is the entire context of when and where the <strong>print()<\/strong> function is called.<\/p>\n<p><img decoding=\"async\" src=\"\/tutImg\/enumPrint.png\" style=\"width: 50%;\" \/><\/p>\n<p><strong>enums<\/strong> can now support multiple types, so long to Box!<\/p>\n<pre class=\"brush: js;\">\r\nenum&lt;T1, T2&gt; {\r\n  case First(T1)\r\n  case Second(T2)\r\n}<\/pre>\n<p><!-- indirect keyword (not in beta 1, but coming soon) --><\/p>\n<p>The do keyword:<\/p>\n<pre class=\"brush: js;\">\r\ndo {\r\n  \/\/...\r\n} while(someVar<5)\r\n\r\n\/\/ Can now be represented as:\r\nrepeat {\r\n  \/\/...\r\n}\r\n<\/pre>\n<p>Option Sets can now use a standard Set type in Swift 2.0<\/p>\n<pre class=\"brush: js;\">\r\nviewOptions = .Repeat | .CurveEaseIn\r\nviewOptions = nil\r\n\r\n\/\/ Can now be represented as a set:\r\n\r\nviewOptions = [.Repeat, .CurveEaseIn]\r\nviewOptions = []\r\n<\/pre>\n<p>We can also create our own set types<\/p>\n<p>(Default implementations in protocols)<\/p>\n<pre class=\"brush: js;\">\r\nstruct MyStyle : OptionSetType {\r\n  let rawValue: Int\r\n  static let Bold     = MyStyle(rawValue: 1)\r\n  static let Italic   = MyStyle(rawValue: 2)\r\n}\r\n\r\niStyle.style = []\r\niStyle.style = [.Bold, .Italic]\r\n\r\nif iStyle.style.contains(.Bold) {\r\n  \/\/...\r\n}\r\n<\/pre>\n<p>Function arguments now behave the same way, regardless of if they are global functions, or methods on a data structure.<\/p>\n<p>So now, you provide arguments labels on functions by default:<\/p>\n<pre class=\"brush: js;\">\r\nfunc myFunc(name: String, age: Int) { ... }\r\n\r\nmyFunc(\"John\", age: 35)\r\n<\/pre>\n<p>The # syntax is now gone, previously used to make the external argument name the same as the internal argument name.<\/p>\n<p>In tests, public and internal are visible, via running a special run mode.<\/p>\n<p><strong>Pattern Matching<\/strong><br \/>\nAdded the guard statement which exits the scope in the else statement.<\/p>\n<pre class=\"brush: js;\">\r\nguard let name = json[\"name\"] as? String else {\r\n  return .Second(\"missing name\")\r\n}\r\n\r\n\/\/ You can also combine this is to complex guards\r\nguard let name = json[\"name\"] as? String,\r\n      let year = json[\"year\"] as? Int else\r\n    return .Second(\"bad input\")\r\n}\r\n\/\/ name and year are now String and Int types, not optionals!\r\nreturn .First(name, year)\r\n<\/pre>\n<p>Switch\/case can now be used with inline if statements<\/p>\n<pre class=\"brush: js;\">\r\nswitch bar() {\r\ncase .SomeCase(let value) where value != 42:\r\n  doThing(value)\r\n\r\ndefault: break\r\n}\r\n\r\n\/\/ Can now be represented as\r\nif case .SomeCase(let value) = bar() where value != 42 {\r\n  doSomething(value)\r\n}\r\n<\/pre>\n<p>for in loops now support boolean filters and full pattern matching<\/p>\n<pre class=\"brush: js;\">\r\nfor value in mySequence where value != \"\" {\r\n  doThing(value)\r\n}\r\n\r\nfor case .MyEnumCase(let value) in enumValues {\r\n  doThing(value)\r\n}\r\n<\/pre>\n<p><!--<strong>Availability Checking<\/strong>\n\n<strong>Protocol Extensions<\/strong>\n\n<strong>Error Handling<\/strong>\n\n\n<strong>BONUS: readline()<\/strong>--><\/p>\n<p><strong>BONUS: Example of try-catch for JSON vs invalid JSON<\/strong><\/p>\n<pre class=\"brush:js;\">\r\nlet jsonString = \"{\\\"name\\\":\\\"Fred\\\", \\\"age\\\":40}\"\r\nlet badJsonString = \"This really isn't valid JSON at all\"\r\n\r\nlet jsonData = jsonString.dataUsingEncoding(NSUTF8StringEncoding)!\r\nlet badJsonData = badJsonString.dataUsingEncoding(NSUTF8StringEncoding)!\r\n\r\ndo {\r\n    \/\/ Try parsing some valid JSON\r\n    let parsed = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.AllowFragments)\r\n    print(parsed)\r\n    \r\n    \/\/ Try parsing some invalid JSON\r\n    let otherParsed = try NSJSONSerialization.JSONObjectWithData(badJsonData, options: NSJSONReadingOptions.AllowFragments)\r\n    print(otherParsed)\r\n}\r\ncatch let error as NSError {\r\n    \/\/ Catch fires here, with an NSErrro being thrown from the JSONObjectWithData method\r\n    print(\"A JSON parsing error occurred, here are the details:\\n \\(error)\")\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Monday Apple announced Swift 2, and just about everything announced is an extremely welcome change. This post is a summary of Chris Lattner&#8217;s discussion in the WWDC video &#8220;What&#8217;s new in Swift&#8221;. Now, let&#8217;s run through them&#8230; Fundamentals enums can now be printed to the console and show the actually value instead of just (enum&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_links_to":"","_links_to_target":""},"categories":[25,10,32,71],"tags":[15,34,33],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.13 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Swift 2 - What&#039;s new - Jameson Quave<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jamesonquave.com\/blog\/swift-2-whats-new\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Swift 2 - What&#039;s new - Jameson Quave\" \/>\n<meta property=\"og:description\" content=\"Monday Apple announced Swift 2, and just about everything announced is an extremely welcome change. This post is a summary of Chris Lattner&#8217;s discussion in the WWDC video &#8220;What&#8217;s new in Swift&#8221;. Now, let&#8217;s run through them&#8230; Fundamentals enums can now be printed to the console and show the actually value instead of just (enum...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jamesonquave.com\/blog\/swift-2-whats-new\/\" \/>\n<meta property=\"og:site_name\" content=\"Jameson Quave\" \/>\n<meta property=\"article:published_time\" content=\"2015-06-10T16:07:14+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-06-11T16:00:47+00:00\" \/>\n<meta name=\"author\" content=\"Jameson Quave\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jameson Quave\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/swift-2-whats-new\/\",\"url\":\"https:\/\/jamesonquave.com\/blog\/swift-2-whats-new\/\",\"name\":\"Swift 2 - What's new - Jameson Quave\",\"isPartOf\":{\"@id\":\"https:\/\/jamesonquave.com\/blog\/#website\"},\"datePublished\":\"2015-06-10T16:07:14+00:00\",\"dateModified\":\"2015-06-11T16:00:47+00:00\",\"author\":{\"@id\":\"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/db6184f355c7f4e3b876d0f228c2fcfc\"},\"breadcrumb\":{\"@id\":\"https:\/\/jamesonquave.com\/blog\/swift-2-whats-new\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jamesonquave.com\/blog\/swift-2-whats-new\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/swift-2-whats-new\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jamesonquave.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Swift 2 &#8211; What&#8217;s new\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/#website\",\"url\":\"https:\/\/jamesonquave.com\/blog\/\",\"name\":\"Jameson Quave\",\"description\":\"Using computer technology to educate, and improve lives.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jamesonquave.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/db6184f355c7f4e3b876d0f228c2fcfc\",\"name\":\"Jameson Quave\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d9786c83345117d560bbeab0e1f26814?s=96&d=retro&r=pg\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d9786c83345117d560bbeab0e1f26814?s=96&d=retro&r=pg\",\"caption\":\"Jameson Quave\"},\"sameAs\":[\"http:\/\/jamesonquave.com\"],\"url\":\"https:\/\/jamesonquave.com\/blog\/author\/jquave\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Swift 2 - What's new - Jameson Quave","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jamesonquave.com\/blog\/swift-2-whats-new\/","og_locale":"en_US","og_type":"article","og_title":"Swift 2 - What's new - Jameson Quave","og_description":"Monday Apple announced Swift 2, and just about everything announced is an extremely welcome change. This post is a summary of Chris Lattner&#8217;s discussion in the WWDC video &#8220;What&#8217;s new in Swift&#8221;. Now, let&#8217;s run through them&#8230; Fundamentals enums can now be printed to the console and show the actually value instead of just (enum...","og_url":"https:\/\/jamesonquave.com\/blog\/swift-2-whats-new\/","og_site_name":"Jameson Quave","article_published_time":"2015-06-10T16:07:14+00:00","article_modified_time":"2015-06-11T16:00:47+00:00","author":"Jameson Quave","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Jameson Quave","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/jamesonquave.com\/blog\/swift-2-whats-new\/","url":"https:\/\/jamesonquave.com\/blog\/swift-2-whats-new\/","name":"Swift 2 - What's new - Jameson Quave","isPartOf":{"@id":"https:\/\/jamesonquave.com\/blog\/#website"},"datePublished":"2015-06-10T16:07:14+00:00","dateModified":"2015-06-11T16:00:47+00:00","author":{"@id":"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/db6184f355c7f4e3b876d0f228c2fcfc"},"breadcrumb":{"@id":"https:\/\/jamesonquave.com\/blog\/swift-2-whats-new\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jamesonquave.com\/blog\/swift-2-whats-new\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jamesonquave.com\/blog\/swift-2-whats-new\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jamesonquave.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Swift 2 &#8211; What&#8217;s new"}]},{"@type":"WebSite","@id":"https:\/\/jamesonquave.com\/blog\/#website","url":"https:\/\/jamesonquave.com\/blog\/","name":"Jameson Quave","description":"Using computer technology to educate, and improve lives.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jamesonquave.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/db6184f355c7f4e3b876d0f228c2fcfc","name":"Jameson Quave","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d9786c83345117d560bbeab0e1f26814?s=96&d=retro&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d9786c83345117d560bbeab0e1f26814?s=96&d=retro&r=pg","caption":"Jameson Quave"},"sameAs":["http:\/\/jamesonquave.com"],"url":"https:\/\/jamesonquave.com\/blog\/author\/jquave\/"}]}},"_links":{"self":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts\/1839"}],"collection":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/comments?post=1839"}],"version-history":[{"count":11,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts\/1839\/revisions"}],"predecessor-version":[{"id":1851,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts\/1839\/revisions\/1851"}],"wp:attachment":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/media?parent=1839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/categories?post=1839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/tags?post=1839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}