{"id":29381,"date":"2020-06-29T16:38:42","date_gmt":"2020-06-29T20:38:42","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=29381"},"modified":"2020-06-29T16:38:42","modified_gmt":"2020-06-29T20:38:42","slug":"as-as-and-as-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2020\/06\/29\/as-as-and-as-in-swift\/","title":{"rendered":"as, as?, and as! in Swift"},"content":{"rendered":"<p><a href=\"https:\/\/oleb.net\/2020\/as\/\">Ole Begemann<\/a>:<\/p>\n<blockquote cite=\"https:\/\/oleb.net\/2020\/as\/\">\n<p><code>as?<\/code> and <code>as!<\/code> <a href=\"https:\/\/docs.swift.org\/swift-book\/LanguageGuide\/TypeCasting.html#ID341\">perform downcasting <em>at runtime<\/em><\/a>.<\/p>\n<p>[&#8230;]<\/p>\n<p><code>as?<\/code> and <code>as!<\/code> are the same operation, the only difference being that <code>as!<\/code> force-unwraps its result.<\/p>\n<p>[&#8230;]<\/p>\n<p>In contrast, <code>as<\/code> <em>coerces<\/em> a value to a type <em>at compile time<\/em>.<\/p>\n<p>[&#8230;]<\/p>\n<p>A syntactic nuance: when using <a href=\"https:\/\/docs.swift.org\/swift-book\/ReferenceManual\/Patterns.html#ID425\"><code>as<\/code> in pattern matching<\/a> contexts, the check is performed at runtime (it has to be).<\/p>\n<\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2020\/04\/03\/swift-bridging-of-allheaderfields\/\">Swift Bridging of allHeaderFields<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2019\/03\/14\/netservice-nuthouse\/\">NetService NutHouse<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2019\/02\/22\/swift-subclass-of-nstextstorage-is-slow-because-of-swift-bridging\/\">Swift Subclass of NSTextStorage Is Slow Because of Swift Bridging<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2017\/08\/29\/swift-4-bridging-peephole-for-as-casts\/\">Swift 4: Bridging Peephole for &ldquo;as&rdquo; Casts<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Ole Begemann: as? and as! perform downcasting at runtime. [&#8230;] as? and as! are the same operation, the only difference being that as! force-unwraps its result. [&#8230;] In contrast, as coerces a value to a type at compile time. [&#8230;] A syntactic nuance: when using as in pattern matching contexts, the check is performed at [&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":"2020-06-29T20:38:46Z","apple_news_api_id":"cf09807c-eb7f-467a-9fe8-116c527111b6","apple_news_api_modified_at":"2020-06-29T20:38:46Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AzwmAfOt_Rnqf6BFsUnERtg","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":[46,71,901],"class_list":["post-29381","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/29381","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=29381"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/29381\/revisions"}],"predecessor-version":[{"id":29382,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/29381\/revisions\/29382"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=29381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=29381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=29381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}