This is my research notebook. I'm an OSX / iOS indie developer. After 8 years of Objective-C I really enjoy Swift nowadays. Trying to publish all my research on Development, Swift & other technologies here.
let success = Result<String, NSError>.Success("success")
With Swift 2 and the introduction of try / catch exception handling. Internally, this doesn't use expensive stack unwinding, as other (say, Objective-C or Java) do it, but instead seems to pretty much return something akin to Either or Result. Only the syntax hides this from the user in order to make it simpler to use1.
1 Swift 2 b5 and earlier
However, once you start using the new do / try / catch more, what happens from time to time is that you start nesting code into messy branches because do is (was) incompatible with the other major way of handling potentially unknown states: optionals. Here's a particular ugly piece of code. Observe how we're nesting if let with let with do with let2.
import Foundation
// get the currently logged in user
func loggedInUser() -> Int? { return 0 }
// get his name
func getUserName (userId: Int) throws -> String { return "Claus" }
// create a new image post with this username. Returns the post data
func imagePostForUserName(name: String, imageURL: NSURL?) -> NSData? { return NSData() }
// post the data to a server
func postImage(data: NSData) throws -> Bool { return true }
if let uid = loggedInUser() {
do {
let username = try getUserName(uid)
if let data = imagePostForUserName(username, imageURL: nil) {
do {
let success = try postImage(data)
if success {
print ("Submitted")
}
} catch {
// more error handling
}
}
} catch {
// todo: error handling
}
}
One reason why this is difficult to simplify is that the do forces a break in any multi guard or multi let3.
2 Swift 2 b6
With beta 6, we get a new keyword, try? which performs a throwing operation and returns optional None in case of failure and optional Some in case of success. Quoting straight from the changelog:
A new keyword 'try?' has been added to Swift. 'try?' attempts to perform an operation that may throw. If the operation succeeds, the result is wrapped in an optional; if it fails (I.e. if an error is thrown), the result is 'nil' and the error is discarded. ‘try?’ is particularly useful when used in conjunction with “if let” and “guard”.
This makes it possible to retrieve the value of a potential throwing operation as an optional. If we apply this to our code above, we can simplify it quite a bit:
if let uid = loggedInUser(),
username = try? getUserName(uid),
data = imagePostForUserName(username, imageURL: nil),
success = try? postImage(data)
where success == true {
print ("Submitted")
}
Submitted
This is, of course a bit of a contrived example, engineered to explain try?. But still, that's definitely less code.
We're, of course, loosing a lot of possibly valuable error information that would otherwise be available in the catch.
3 Which to choose?
try? can help you write terser code at the expense of loosing insights. Using try? only returns an optional without further information on the particular cause of the error / exception. The benefit, of course, is beautiful composability with a lot of the other Swift syntax, like map, flatmap, switch, guard, if let, for case, and others.
The non-optional try works great for distinct task where you're not dependant on earlier or later possible optional results.
The aforementioned Result type, on the other hand offers both; either the requested value, or a possible error. You can just continue using Result, which also has support for wrapping throws and much more, however keep in mind that this is not the direction Swift seems to intend to go 4. Otherwise, we'd have a full blown Result or Either type in Swift 2.
I'm really happy about the introduction of try? as it will make many snippets, particularly when interacting with the Cocoa API, easier to solve.
{:description
"Swift 2.0 b6 includes a new way of handling exceptions via the =try?= keyword. This is a quick post to explain the basics, and why this is cool.",
:keyword-tags (:swift),
:projects ({:project "Sarbatka", :link "/electronic-music.html"}),
:postlist
({:title "Blogstrapped.",
:url "/2011/12/01/blogstrapped/",
:tags "entrepeneurship blog",
:keyword-tags (:entrepeneurship :blog),
:date "Thu, 1 Dec 2011",
:keywords "entrepeneurship videro fear unknown stylemac",
:keyword-keywords
(:entrepeneurship :videro :fear :unknown :stylemac)}
{:title "Fast NSDictionary traversal in Objective-C",
:url "/2011/12/07/fast-nsdictionary-traversal-in-objective-c/",
:tags "objective-c ios cocoa",
:keyword-tags (:objective-c :ios :cocoa),
:date "Wed, 7 Dec 2011"}
{:title "How the iPad can improve focus",
:url "/2011/12/09/how-the-ipad-can-improve-focus/",
:tags "opinion",
:keyword-tags (:opinion),
:date "Fri, 9 Dec 2011"}
{:title "Use VIM as an Xcode alternative",
:url "/2013/01/29/use-vim-as-xcode-alternative-ios-mac-cocoa/",
:tags "cocoa objective-c ios",
:keyword-tags (:cocoa :objective-c :ios),
:date "Tue, 29 Jan 2013"}
{:title "Now Running Clojure",
:url "/2014/01/20/now-running-clojure/",
:tags "clojure blog",
:keyword-tags (:clojure :blog),
:date "Mon, 20 Jan 2014"}
{:title
"Debugging entitlement issues in Maps, iCloud, In-App, Keychain, or GameCenter",
:url
"/2014/01/21/debugging-entitlement-maps-icloud-gamecenter-issues/",
:tags "ios cocoa",
:keyword-tags (:ios :cocoa),
:date "Tue, 21 Jan 2014",
:keywords "ios cocoa entitlements",
:keyword-keywords (:ios :cocoa :entitlements)}
{:title
"Clojure/Enlive Static Site Generator that keeps your HTML intact",
:url
"/2014/01/22/clojure-enlive-static-site-generator-that-keeps-html-intact/",
:tags "blog clojure",
:keyword-tags (:blog :clojure),
:date "Wed, 22 Jan 2014",
:keywords "clojure static site generator jekyll html enlive",
:keyword-keywords
(:clojure :static :site :generator :jekyll :html :enlive)}
{:title "Finding a good URL Partitioning Scheme for PostgreSQL",
:url "/2014/01/24/finding-url-partitioning-scheme-postgres/",
:tags "postgresql clojure",
:keyword-tags (:postgresql :clojure),
:date "Fri, 24 Jan 2014",
:keywords "clojure postgresql partitioning scheme",
:keyword-keywords (:clojure :postgresql :partitioning :scheme)}
{:title "An Emacs Lisp tooling tutorial, writing a bulk mailer",
:url "/2014/01/29/emacs-lisp-tooling-tutorial-writing-bulk-mailer/",
:tags "emacs",
:keyword-tags (:emacs),
:date "Wed, 29 Jan 2014",
:keywords
"emacs lisp bulk mailer tutorial email vim vimscript evil",
:keyword-keywords
(:emacs :lisp :bulk :mailer :tutorial :email :vim :vimscript :evil)}
{:title
"Creating a Swift syntax extension: the Lisp 'cond' function",
:url "/2014/06/08/writing-simple-syntax-extensions-in-swift/",
:tags "swift ios cocoa",
:keyword-tags (:swift :ios :cocoa),
:date "Sun, 8 Jun 2014",
:keywords
"clojure lisp swift cond syntax macro extension cocoa ios feature",
:keyword-keywords
(:clojure
:lisp
:swift
:cond
:syntax
:macro
:extension
:cocoa
:ios
:feature)}
{:title "Swift optionals made simple",
:url "/2014/06/13/swift-optionals-made-simple/",
:tags "swift ios cocoa",
:keyword-tags (:swift :ios :cocoa),
:date "Fri, 13 Jun 2014",
:keywords
"lisp swift optional scala simple optionals switch chaining feature",
:keyword-keywords
(:lisp
:swift
:optional
:scala
:simple
:optionals
:switch
:chaining
:feature)}
{:title "Generic method overloading by protocol in Swift",
:url "/2015/06/17/swift-method-overloading-by-protocol/",
:tags "swift ios cocoa",
:keyword-tags (:swift :ios :cocoa),
:date "Wed, 17 Jun 2015",
:keywords
"swift optional simple overloading method protocol extensions generics feature",
:keyword-keywords
(:swift
:optional
:simple
:overloading
:method
:protocol
:extensions
:generics
:feature)}
{:title "Using try / catch in Swift with asynchronous closures",
:url "/2015/06/19/swift-try-catch-asynchronous-closures/",
:tags "swift ios",
:keyword-tags (:swift :ios),
:date "Fri, 19 Jun 2015",
:keywords "swift try catch errortype closure async result feature",
:keyword-keywords
(:swift :try :catch :errortype :closure :async :result :feature)}
{:title "Debugging advanced compilation errors in ClojureScript",
:url "/2015/07/02/debugging-clojurescript-advanced-compilation/",
:tags "clojure",
:keyword-tags (:clojure),
:date "Thu, 2 Jul 2015",
:keywords "debugging clojure clojurescript externs",
:keyword-keywords (:debugging :clojure :clojurescript :externs)}
{:title "Tuples in Swift, Advanced Usage and Best Practices",
:url "/2015/07/19/tuples-swift-advanced-usage-best-practices/",
:tags "swift",
:keyword-tags (:swift),
:date "Sun, 19 Jul 2015",
:keywords "swift tuples generics feature",
:keyword-keywords (:swift :tuples :generics :feature)}
{:title "Match Me if you can: Swift Pattern Matching in Detail.",
:url "/2015/08/20/swift-pattern-matching-in-detail/",
:tags "swift ios cocoa",
:keyword-tags (:swift :ios :cocoa),
:date "Thu, 20 Aug 2015",
:keywords
"feature lisp swift optional scala simple optionals switch chaining for pattern matching clojure haskell",
:keyword-keywords
(:feature
:lisp
:swift
:optional
:scala
:simple
:optionals
:switch
:chaining
:for
:pattern
:matching
:clojure
:haskell)}
{:title "Optional throw via try? in Swift 2 beta 6",
:url "/2015/08/25/optional-throw-swift/",
:tags "swift",
:keyword-tags (:swift),
:date "Tue, 25 Aug 2015",
:keywords
"swift error throw result either rethrow try syntax swift2",
:keyword-keywords
(:swift
:error
:throw
:result
:either
:rethrow
:try
:syntax
:swift2)}
{:title "Getting your iPhone 6s Chip Foundry from Swift",
:url "/2015/09/30/getting-iphone6s-foundry-from-swift/",
:tags "swift",
:keyword-tags (:swift),
:date "Wed, 30 Sep 2015",
:keywords
"swift iphone6s iphone tsmc samsung gestalt private api foundation",
:keyword-keywords
(:swift
:iphone6s
:iphone
:tsmc
:samsung
:gestalt
:private
:api
:foundation)}
{:title "Advanced & Practical Enum usage in Swift",
:url "/2015/10/17/advanced-practical-enum-examples/",
:tags "swift cocoa ios",
:keyword-tags (:swift :cocoa :ios),
:date "Sat, 17 Oct 2015",
:keywords
"feature swift enum algebraic caseclass union case switch pattern simple practical advanced example",
:keyword-keywords
(:feature
:swift
:enum
:algebraic
:caseclass
:union
:case
:switch
:pattern
:simple
:practical
:advanced
:example)}
{:title "The Swift Reflection API and what you can do with it",
:url "/2015/10/24/swift-reflection-api-what-you-can-do/",
:tags "swift cocoa ios",
:keyword-tags (:swift :cocoa :ios),
:date "Sat, 24 Oct 2015",
:keywords
"feature swift reflection struct class displayType mirror api reflecting any anyobject",
:keyword-keywords
(:feature
:swift
:reflection
:struct
:class
:displayType
:mirror
:api
:reflecting
:any
:anyobject)}
{:title "Reduce all the things",
:url "/2015/11/30/reduce-all-the-things/",
:tags "swift cocoa ios",
:keyword-tags (:swift :cocoa :ios),
:date "Mon, 30 Nov 2015",
:keywords
"feature swift reduce map filter group partition split interpose chunk functional programming flatMap",
:keyword-keywords
(:feature
:swift
:reduce
:map
:filter
:group
:partition
:split
:interpose
:chunk
:functional
:programming
:flatMap)}
{:title
"Swift Package Manager: Create and Use a X11 package on Linux",
:url "/2015/12/08/swift-ubuntu-x11-window-app/",
:tags "swift linux",
:keyword-tags (:swift :linux),
:date "Tue, 8 Dec 2015",
:keywords "linux x11 swift libx11 xserver xorg",
:keyword-keywords (:linux :x11 :swift :libx11 :xserver :xorg)}
{:title "Hirundo: Comfortably follow Swift Mailing Lists on OSX",
:url "/2016/02/02/hirundo-mac-app-swift-mailing-lists/",
:tags "swift cocoa ios",
:keyword-tags (:swift :cocoa :ios),
:date "Tue, 2 Feb 2016",
:keywords
"swift mac cocoa mailing list swift-dev swift-eveolution swift-users reading macosx",
:keyword-keywords
(:swift
:mac
:cocoa
:mailing
:list
:swift-dev
:swift-eveolution
:swift-users
:reading
:macosx)}
{:title "Three tips for concise Swift using the Guard statement",
:url "/2016/03/29/three-tips-for-clean-swift-code/",
:tags "swift cocoa ios",
:keyword-tags (:swift :cocoa :ios),
:date "Tue, 29 Mar 2016",
:keywords
"swift mac cocoa guard let enum pattern matching patterns",
:keyword-keywords
(:swift :mac :cocoa :guard :let :enum :pattern :matching :patterns)}
{:title "Using Git Hooks to prevent commiting test code",
:url "/2016/04/04/prevent-accidental-test-code-commits/",
:tags "git",
:keyword-tags (:git),
:date "Mon, 4 Apr 2016",
:keywords "git hook commit debug test code",
:keyword-keywords (:git :hook :commit :debug :test :code)}
{:title
"Force optionals in multi-unwrapped \"guard let\" or \"if let\"",
:url
"/2016/04/14/force-optionals-in-guard-or-let-optional-binding/",
:tags "swift cocoa ios",
:keyword-tags (:swift :cocoa :ios),
:date "Thu, 14 Apr 2016",
:keywords
"swift guard let unwrap bind binding unwrapped optional some none optionals",
:keyword-keywords
(:swift
:guard
:let
:unwrap
:bind
:binding
:unwrapped
:optional
:some
:none
:optionals)}
{:title "Raw value initializers for enums with associated types",
:url "/2016/04/23/associated-types-enum-raw-value-initializers/",
:tags "swift cocoa ios",
:keyword-tags (:swift :cocoa :ios),
:date "Sat, 23 Apr 2016",
:keywords
"swift optional enum raw value initializers associated type",
:keyword-keywords
(:swift
:optional
:enum
:raw
:value
:initializers
:associated
:type)}
{:title "SwiftWatch",
:url "/2016/04/28/swiftwatch/",
:tags "swift cocoa ios",
:keyword-tags (:swift :cocoa :ios),
:date "Thu, 28 Apr 2016",
:keywords
"swift hackernews reddit designernews lamernews socialnews swiftlang programming community post vote comment",
:keyword-keywords
(:swift
:hackernews
:reddit
:designernews
:lamernews
:socialnews
:swiftlang
:programming
:community
:post
:vote
:comment)}
{:title "Data in Swift 3 parsing a Doom WAD File",
:url "/2016/07/15/swift3-nsdata-data/",
:tags "swift cocoa ios",
:keyword-tags (:swift :cocoa :ios),
:date "Fri, 15 Jul 2016",
:keywords "swift doom wad lumps data nsdata swift3 binary bytes",
:keyword-keywords
(:swift :doom :wad :lumps :data :nsdata :swift3 :binary :bytes)}),
:tags
"benedikt, c, clojure, clojurescript, cocoa, either, error, html, ios, javascript, mac, objective-c, photodesk, research, result, rethrow, stylemac, swift, swift2, syntax, terhechte, throw, try",
:type :post,
:keywords "swift error throw result either rethrow try syntax swift2",
:title "Optional throw via try? in Swift 2 beta 6",
:author "Benedikt Terhechte",
:feature-image
"/img-content/2015-08-25-optional-throw-swift-feature-image.jpg",
:categories
({:tag "blog", :url "/tags/blog/index.html", :count 3}
{:tag "clojure", :url "/tags/clojure/index.html", :count 4}
{:tag "cocoa", :url "/tags/cocoa/index.html", :count 16}
{:tag "emacs", :url "/tags/emacs/index.html", :count 1}
{:tag "entrepeneurship",
:url "/tags/entrepeneurship/index.html",
:count 1}
{:tag "git", :url "/tags/git/index.html", :count 1}
{:tag "ios", :url "/tags/ios/index.html", :count 17}
{:tag "linux", :url "/tags/linux/index.html", :count 1}
{:tag "objective-c", :url "/tags/objective-c/index.html", :count 2}
{:tag "opinion", :url "/tags/opinion/index.html", :count 1}
{:tag "postgresql", :url "/tags/postgresql/index.html", :count 1}
{:tag "swift", :url "/tags/swift/index.html", :count 18}),
:url "/2015/08/25/optional-throw-swift/",
:options "toc:nil ^:{} ",
:blog-index nil,
:watching nil,
:site-title "Appventure",
:keyword-keywords
(:swift :error :throw :result :either :rethrow :try :syntax :swift2)}
[{:keyword-tags (:swift),
:tags "swift",
:date "Tue, 25 Aug 2015",
:footnotes nil,
:meta
{:feature-image
"/img-content/2015-08-25-optional-throw-swift-feature-image.jpg",
:title "Optional throw via try? in Swift 2 beta 6",
:keyword-tags (:swift),
:tags "swift",
:keyword-keywords
(:swift
:error
:throw
:result
:either
:rethrow
:try
:syntax
:swift2),
:keywords
"swift error throw result either rethrow try syntax swift2",
:description
"Swift 2.0 b6 includes a new way of handling exceptions via the =try?= keyword. This is a quick post to explain the basics, and why this is cool.",
:options "toc:nil ^:{} "},
:content
"<!-- #+feature-image: /img-content/2015-08-25-optional-throw-swift-feature-image.jpg -->\n\n<h6><a href=\"http://swift.gg/2015/09/11/optional-throw-swift/\">This post is also available in <b>🇨🇳Chinese</b></a><span> Thanks to </span><a href=\"http://swift.gg/tags/APPVENTURE/\">SwiftGG</a></h6>\n\n<p>\nSwift 2.0 b6 includes a new way of handling exceptions via the <code>try?</code> keyword. This is a quick post to explain the basics, and why this is cool.\n</p>\n\n<p>\nIn Swift 1.x, all we had for error handling were optionals and <code>NSError</code>. Which is <a href=\"https://github.com/antitypical/Result\">why many people adopted <code>Either</code> / <code>Result</code> types</a> as they can be found in <a href=\"https://hackage.haskell.org/package/base-4.8.1.0/docs/Data-Either.html\">other</a> <a href=\"http://www.scala-lang.org/api/2.9.3/scala/Either.html\">programming</a> languages:\n</p>\n\n<div class=\"org-src-container\">\n\n<pre class=\"src src-swift\">let success = Result<String, NSError>.Success(\"success\")\n</pre>\n</div>\n\n<p>\nWith Swift 2 and the introduction of <code>try / catch</code> exception handling. Internally, this doesn't use expensive stack unwinding, as other (say, Objective-C or Java) do it, but instead seems to pretty much return something akin to <code>Either</code> or <code>Result</code>. Only the syntax hides this from the user in order to make it simpler to use<sup><a id=\"fnr.1\" name=\"fnr.1\" class=\"footref\" href=\"#fn.1\">1</a></sup>.\n</p>\n\n<div id=\"outline-container-sec-1\" class=\"outline-2\">\n<h2 id=\"sec-1\"><span class=\"section-number-2\">1</span> Swift 2 b5 and earlier</h2>\n<div class=\"outline-text-2\" id=\"text-1\">\n<p>\nHowever, once you start using the new <code>do / try / catch</code> more, what happens from time to time is that you start nesting code into messy branches because <code>do</code> is (was) incompatible with the other major way of handling potentially unknown states: optionals. Here's a particular ugly piece of code. Observe how we're nesting <code>if let</code> with <code>let</code> with <code>do</code> with <code>let</code> <sup><a id=\"fnr.2\" name=\"fnr.2\" class=\"footref\" href=\"#fn.2\">2</a></sup>.\n</p>\n\n<div class=\"org-src-container\">\n\n<pre class=\"src src-swift\">import Foundation\n// get the currently logged in user\nfunc loggedInUser() -> Int? { return 0 }\n// get his name\nfunc getUserName (userId: Int) throws -> String { return \"Claus\" }\n// create a new image post with this username. Returns the post data\nfunc imagePostForUserName(name: String, imageURL: NSURL?) -> NSData? { return NSData() }\n// post the data to a server\nfunc postImage(data: NSData) throws -> Bool { return true }\n\nif let uid = loggedInUser() {\n do {\n\tlet username = try getUserName(uid)\n\tif let data = imagePostForUserName(username, imageURL: nil) {\n\t do {\n\t\tlet success = try postImage(data)\n\t\tif success {\n\t\t print (\"Submitted\")\n\t\t} \n\t } catch {\n\t\t// more error handling\n\t }\n\t}\n } catch {\n\t// todo: error handling\n }\n}\n</pre>\n</div>\n\n<p>\nOne reason why this is difficult to simplify is that the <code>do</code> forces a break in any multi <code>guard</code> or multi <code>let</code> <sup><a id=\"fnr.3\" name=\"fnr.3\" class=\"footref\" href=\"#fn.3\">3</a></sup>.\n</p>\n</div>\n</div>\n\n<div id=\"outline-container-sec-2\" class=\"outline-2\">\n<h2 id=\"sec-2\"><span class=\"section-number-2\">2</span> Swift 2 b6</h2>\n<div class=\"outline-text-2\" id=\"text-2\">\n<p>\nWith beta 6, we get a new keyword, <code>try?</code> which performs a throwing operation and returns optional <code>None</code> in case of failure and optional <code>Some</code> in case of success. <a href=\"http://adcdownload.apple.com/Developer_Tools/Xcode_7_beta_6/Xcode_7_beta_6_Release_Notes.pdf\">Quoting straight from the changelog:</a>\n</p>\n\n<blockquote>\n<p>\nA new keyword 'try?' has been added to Swift. 'try?' attempts to perform an operation that may throw. If the operation succeeds, the result is wrapped in an optional; if it fails (I.e. if an error is thrown), the result is 'nil' and the error is discarded. ‘try?’ is particularly useful when used in conjunction with “if let” and “guard”.\n</p>\n</blockquote>\n\n<p>\nThis makes it possible to retrieve the value of a potential throwing operation as an optional. If we apply this to our code above, we can simplify it quite a bit:\n</p>\n\n<div class=\"org-src-container\">\n\n<pre class=\"src src-swift\" id=\"feature-image\">if let uid = loggedInUser(),\n username = try? getUserName(uid),\n data = imagePostForUserName(username, imageURL: nil),\n success = try? postImage(data)\n where success == true {\n print (\"Submitted\")\n}\n</pre>\n</div>\n\n<pre class=\"example\">\nSubmitted\n</pre>\n\n<p>\nThis is, of course a bit of a contrived example, engineered to explain <code>try?</code>. But still, that's definitely less code. \nWe're, of course, loosing a lot of possibly valuable error information that would otherwise be available in the <code>catch</code>.\n</p>\n</div>\n</div>\n\n<div id=\"outline-container-sec-3\" class=\"outline-2\">\n<h2 id=\"sec-3\"><span class=\"section-number-2\">3</span> Which to choose?</h2>\n<div class=\"outline-text-2\" id=\"text-3\">\n<p>\n<code>try?</code> can help you write terser code at the expense of loosing insights. Using <code>try?</code> only returns an optional without further information on the particular cause of the error / exception. The benefit, of course, is beautiful composability with <b>a lot</b> of the other Swift syntax, like <code>map</code>, <code>flatmap</code>, <code>switch</code>, <code>guard</code>, <code>if let</code>, <code>for case</code>, and others.\n</p>\n\n\n<p>\nThe non-optional <code>try</code> works great for distinct task where you're not dependant on earlier or later possible optional results.\n</p>\n\n<p>\nThe aforementioned <code>Result</code> type, on the other hand offers both; either the requested value, or a possible error. You can just continue using <code>Result</code>, which also has support for wrapping throws and much more, however keep in mind that this is not the direction Swift seems to intend to go <sup><a id=\"fnr.4\" name=\"fnr.4\" class=\"footref\" href=\"#fn.4\">4</a></sup>. Otherwise, we'd have a full blown Result or Either type in Swift 2.\n</p>\n\n<p>\nI'm really happy about the introduction of <code>try?</code> as it will make many snippets, particularly when interacting with the Cocoa API, easier to solve.\n</p>\n</div>\n</div>\n<div id=\"footnotes\">\n<h2 class=\"footnotes\">Footnotes: </h2>\n<div id=\"text-footnotes\">\n\n<div class=\"footdef\"><sup><a id=\"fn.1\" name=\"fn.1\" class=\"footnum\" href=\"#fnr.1\">1</a></sup> <p>Much like the lifting into / out of monads in Swift optionals is hidden via the <code>?</code> syntax</p></div>\n\n<div class=\"footdef\"><sup><a id=\"fn.2\" name=\"fn.2\" class=\"footnum\" href=\"#fnr.2\">2</a></sup> <p>There're ways to improve this code without <code>try?</code>, of course, but it makes for a nice example</p></div>\n\n<div class=\"footdef\"><sup><a id=\"fn.3\" name=\"fn.3\" class=\"footnum\" href=\"#fnr.3\">3</a></sup> <p>Another is, of course, that I'm using raw NSRegularExpression here, instead of a simplifying library</p></div>\n\n<div class=\"footdef\"><sup><a id=\"fn.4\" name=\"fn.4\" class=\"footnum\" href=\"#fnr.4\">4</a></sup> <p>Also, you'll always need to add additional dependencies to your project</p></div>\n\n\n</div>\n</div>",
:keywords
"swift error throw result either rethrow try syntax swift2",
:title "Optional throw via try? in Swift 2 beta 6",
:filename "2015-08-25-optional-throw-swift.org",
:id -1919280999,
:url "/2015/08/25/optional-throw-swift/",
:javadate #inst "2015-08-24T22:00:00.000-00:00",
:keyword-keywords
(:swift :error :throw :result :either :rethrow :try :syntax :swift2)}]