{"id":796,"date":"2014-06-14T12:20:55","date_gmt":"2014-06-14T18:20:55","guid":{"rendered":"http:\/\/jamesonquave.com\/blog\/?p=796"},"modified":"2017-04-03T18:49:36","modified_gmt":"2017-04-04T00:49:36","slug":"understanding-the-fatal-error-cant-unwrap-optional-none-errors-in-swift","status":"publish","type":"post","link":"https:\/\/jamesonquave.com\/blog\/understanding-the-fatal-error-cant-unwrap-optional-none-errors-in-swift\/","title":{"rendered":"Swift: &#8220;fatal error: unexpectedly found nil while unwrapping an Optional value&#8221; errors in Swift"},"content":{"rendered":"<h3>fatal error: unexpectedly found nil while unwrapping an Optional value<\/h3>\n<p>Have you seen this yet working with Swift yet? If so, you&#8217;re not alone. In this post I&#8217;m going to dig in to what this means, and explain a bit about optionals in Swift. For those of you who are experienced programmers and have arrived at this page trying to figure out what the error means, here&#8217;s the short version:<\/p>\n<p><b>It means your variable is set to nil, but your code is expecting it to not be nil.<\/b><\/p>\n<p>To help understand optionals, let&#8217;s open up a Swift REPL. Open up Terminal and boot up the REPL:<\/p>\n<p><code>swift<\/code><\/p>\n<p>Let&#8217;s get this error to show up, and analyze what&#8217;s happening.<br \/>\nFirst, create an optional with nothing set:<\/p>\n<p><code>var username: String?<\/code><br \/>\n<i>username: String? = nil<\/i><\/p>\n<p>From the REPL it&#8217;s clear that username is set to nil, in Xcode it&#8217;s not always as obvious. Let&#8217;s go ahead and try to use the variable:<\/p>\n<p><code>print(username)<\/code><br \/>\n<i>nil<\/i><\/p>\n<p>Okay, confirmed. username is nil. print() understands this and just tells us as much. But, what if we want to make it uppercase using username.uppercaseString? First, let&#8217;s try it with a regular ol&#8217; string.<\/p>\n<p><code><br \/>\n\"Jameson\".uppercaseString<br \/>\n<\/code><br \/>\n<i>String = &#8220;JAMESON&#8221;<\/i><\/p>\n<p>Cool, it works. Now let&#8217;s try it on username:<\/p>\n<p><code><br \/>\nusername.uppercaseString<br \/>\n<\/code><br \/>\n<i>error: value of optional type &#8216;String?&#8217; not unwrapped; did you mean to use &#8216;!&#8217; or &#8216;?&#8217;?<\/i><\/p>\n<p>Oops, we can&#8217;t use uppercaseString because it&#8217;s not available in the Optional String?<br \/>\nWe need to get an &#8220;unwrapped&#8221; String variable in order to use uppercaseString, because otherwise what we&#8217;re seeing is an optional value. Think of this like a &#8220;box&#8221; we keep values inside of. The box may be empty, or it may contain the value we want. But to be sure, and to work with the internals, we need to force unwrap it by adding an exclamation mark to the end.<\/p>\n<p><code><br \/>\nusername!.uppercaseString<br \/>\n<\/code><br \/>\n<i>fatal error: unexpectedly found nil while unwrapping an Optional value<\/i><\/p>\n<p>Because we tried to force unwrap the optional without ever setting a value to it, we get this error. So what is username? We never set it! This is what the optional type protects from (ourselves)<\/p>\n<p>Optional is actually an enum that contains some information on the variable that&#8217;s &#8220;inside&#8221; it.<\/p>\n<p>When it contains something, the case is &#8216;Some&#8217;, and when it&#8217;s empty it&#8217;s value is &#8216;None&#8217;. Or in other words.. the Swift compiler is asked to work with what&#8217;s inside the box, and if it&#8217;s empty, it&#8217;s case is &#8220;None&#8221;. Otherwise, it&#8217;s case is &#8220;Some&#8221; with an associated value, like a username.<\/p>\n<p>So what should you do about it? It&#8217;s pretty simple, just use optional binding. This unwraps an optional safely and allows us to execute a block of code *only* if the optional actually contained a value. It looks like this:<\/p>\n<p><code><br \/>\nif let username = username {<br \/>\nprint(username)<br \/>\n}<br \/>\n<\/code><\/p>\n<p>If you type this in to the REPL, you&#8217;ll see nothing happen. Why? Well, because username is &#8220;None&#8221;! It contains nothing. Try it again with a value set:<\/p>\n<p><code><br \/>\nlet username: String? = \"jquave\"<br \/>\nif let usernameUnwrapped = username {<br \/>\nprint(usernameUnwrapped)<br \/>\n}<br \/>\n<\/code><\/p>\n<p><strong>jquave<\/strong><\/p>\n<p>So there you go. Just use optional binding! If you want to unwrap a bunch of variables before proceeding, you can chain these bindings together with commas. So for example if we had to get both a username and password it would look like this:<\/p>\n<p><code><br \/>\nlet username: String? = \"jquave\"<br \/>\nlet password: String? = \"romneycomeback\"<br \/>\nif let usernameUnwrapped = username, let passwordUnwrapped = password {<br \/>\nprint(usernameUnwrapped)<br \/>\nprint(passwordUnwrapped)<br \/>\n}<br \/>\n<\/code><\/p>\n<p>If you&#8217;re interested in learning more about Swift, be sure to <a href=\"http:\/\/jamesonquave.us6.list-manage.com\/subscribe\/post?u=1d2576bf288fe2fd7fa71bd20&amp;id=6c787ed58a\">subscribe to my mailing list<\/a> for updates. I also recently released a complete <a href=\"http:\/\/jamesonquave.com\/blog\/swift-3-tutorial-fundamentals\/\">Swift 3 Fundamentals Tutorial<\/a> that covers the language in greater detail.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>fatal error: unexpectedly found nil while unwrapping an Optional value Have you seen this yet working with Swift yet? If so, you&#8217;re not alone. In this post I&#8217;m going to dig in to what this means, and explain a bit about optionals in Swift. For those of you who are experienced programmers and have arrived&#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":[10,32],"tags":[15,33],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.13 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Swift: &quot;fatal error: unexpectedly found nil while unwrapping an Optional value&quot; errors in Swift - 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\/understanding-the-fatal-error-cant-unwrap-optional-none-errors-in-swift\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Swift: &quot;fatal error: unexpectedly found nil while unwrapping an Optional value&quot; errors in Swift - Jameson Quave\" \/>\n<meta property=\"og:description\" content=\"fatal error: unexpectedly found nil while unwrapping an Optional value Have you seen this yet working with Swift yet? If so, you&#8217;re not alone. In this post I&#8217;m going to dig in to what this means, and explain a bit about optionals in Swift. For those of you who are experienced programmers and have arrived...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jamesonquave.com\/blog\/understanding-the-fatal-error-cant-unwrap-optional-none-errors-in-swift\/\" \/>\n<meta property=\"og:site_name\" content=\"Jameson Quave\" \/>\n<meta property=\"article:published_time\" content=\"2014-06-14T18:20:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-04-04T00:49:36+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=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/understanding-the-fatal-error-cant-unwrap-optional-none-errors-in-swift\/\",\"url\":\"https:\/\/jamesonquave.com\/blog\/understanding-the-fatal-error-cant-unwrap-optional-none-errors-in-swift\/\",\"name\":\"Swift: \\\"fatal error: unexpectedly found nil while unwrapping an Optional value\\\" errors in Swift - Jameson Quave\",\"isPartOf\":{\"@id\":\"https:\/\/jamesonquave.com\/blog\/#website\"},\"datePublished\":\"2014-06-14T18:20:55+00:00\",\"dateModified\":\"2017-04-04T00:49:36+00:00\",\"author\":{\"@id\":\"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/db6184f355c7f4e3b876d0f228c2fcfc\"},\"breadcrumb\":{\"@id\":\"https:\/\/jamesonquave.com\/blog\/understanding-the-fatal-error-cant-unwrap-optional-none-errors-in-swift\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jamesonquave.com\/blog\/understanding-the-fatal-error-cant-unwrap-optional-none-errors-in-swift\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/understanding-the-fatal-error-cant-unwrap-optional-none-errors-in-swift\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jamesonquave.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Swift: &#8220;fatal error: unexpectedly found nil while unwrapping an Optional value&#8221; errors in Swift\"}]},{\"@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: \"fatal error: unexpectedly found nil while unwrapping an Optional value\" errors in Swift - 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\/understanding-the-fatal-error-cant-unwrap-optional-none-errors-in-swift\/","og_locale":"en_US","og_type":"article","og_title":"Swift: \"fatal error: unexpectedly found nil while unwrapping an Optional value\" errors in Swift - Jameson Quave","og_description":"fatal error: unexpectedly found nil while unwrapping an Optional value Have you seen this yet working with Swift yet? If so, you&#8217;re not alone. In this post I&#8217;m going to dig in to what this means, and explain a bit about optionals in Swift. For those of you who are experienced programmers and have arrived...","og_url":"https:\/\/jamesonquave.com\/blog\/understanding-the-fatal-error-cant-unwrap-optional-none-errors-in-swift\/","og_site_name":"Jameson Quave","article_published_time":"2014-06-14T18:20:55+00:00","article_modified_time":"2017-04-04T00:49:36+00:00","author":"Jameson Quave","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Jameson Quave","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/jamesonquave.com\/blog\/understanding-the-fatal-error-cant-unwrap-optional-none-errors-in-swift\/","url":"https:\/\/jamesonquave.com\/blog\/understanding-the-fatal-error-cant-unwrap-optional-none-errors-in-swift\/","name":"Swift: \"fatal error: unexpectedly found nil while unwrapping an Optional value\" errors in Swift - Jameson Quave","isPartOf":{"@id":"https:\/\/jamesonquave.com\/blog\/#website"},"datePublished":"2014-06-14T18:20:55+00:00","dateModified":"2017-04-04T00:49:36+00:00","author":{"@id":"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/db6184f355c7f4e3b876d0f228c2fcfc"},"breadcrumb":{"@id":"https:\/\/jamesonquave.com\/blog\/understanding-the-fatal-error-cant-unwrap-optional-none-errors-in-swift\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jamesonquave.com\/blog\/understanding-the-fatal-error-cant-unwrap-optional-none-errors-in-swift\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jamesonquave.com\/blog\/understanding-the-fatal-error-cant-unwrap-optional-none-errors-in-swift\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jamesonquave.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Swift: &#8220;fatal error: unexpectedly found nil while unwrapping an Optional value&#8221; errors in Swift"}]},{"@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\/796"}],"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=796"}],"version-history":[{"count":22,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts\/796\/revisions"}],"predecessor-version":[{"id":2234,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts\/796\/revisions\/2234"}],"wp:attachment":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/media?parent=796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/categories?post=796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/tags?post=796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}