{"id":18274,"date":"2017-06-29T15:18:49","date_gmt":"2017-06-29T19:18:49","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=18274"},"modified":"2017-06-29T15:18:49","modified_gmt":"2017-06-29T19:18:49","slug":"banning-force-unwrapping-swift-optionals","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/06\/29\/banning-force-unwrapping-swift-optionals\/","title":{"rendered":"Banning Force Unwrapping Swift Optionals"},"content":{"rendered":"<p><a href=\"http:\/\/blog.timac.org\/?p=1760\">Alexandre Colucci<\/a>:<\/p>\n<blockquote cite=\"http:\/\/blog.timac.org\/?p=1760\">\n<p>Download a random Sample Code from <a href=\"https:\/\/developer.apple.com\/library\/content\/navigation\/#section=Resource%20Types&amp;topic=Sample%20Code\">Apple Developer website<\/a> and look if and how force unwrapping is used.<\/p>\n<p>[&#8230;]<\/p>\n<p>Xcode&rsquo;s Fix-it feature proposes to insert the explicit unwrap operator&#8230; even when it doesn&rsquo;t make sense[&#8230;]<\/p>\n<p>[&#8230;]<\/p>\n<p>There are multiple solutions that are cleaner and safer than using an explicit unwrap. I also generally find the code more readable. Below are several examples.<\/p>\n<p>[&#8230;]<\/p>\n<p>Similarly to force unwrapping an optional, I see a lot of code dangerously using implicit unwrapped optionals. This kind of optionals should also be avoided&#8230; except in one case where it is extremely useful: When declaring an IBOutlet, you should use an implicit unwrapped optional because once the method viewDidLoad is called, the IBOutlet will be initialized.<\/p>\n<\/blockquote>\n<p>Another case where IUOs are unfortunately needed is in test code, because the variables aren&rsquo;t initialized until set <code>setUp()<\/code> method.<\/p>","protected":false},"excerpt":{"rendered":"<p>Alexandre Colucci: Download a random Sample Code from Apple Developer website and look if and how force unwrapping is used. [&#8230;] Xcode&rsquo;s Fix-it feature proposes to insert the explicit unwrap operator&#8230; even when it doesn&rsquo;t make sense[&#8230;] [&#8230;] There are multiple solutions that are cleaner and safer than using an explicit unwrap. I also generally [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[31,30,71,901,268,226],"class_list":["post-18274","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-ios","tag-mac","tag-programming","tag-swift-programming-language","tag-testing","tag-xcode"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=18274"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18274\/revisions"}],"predecessor-version":[{"id":18275,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18274\/revisions\/18275"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=18274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=18274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=18274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}