{"id":10151,"date":"2014-11-25T14:31:39","date_gmt":"2014-11-25T19:31:39","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=10151"},"modified":"2014-11-25T14:31:39","modified_gmt":"2014-11-25T19:31:39","slug":"strange-tales-of-swift-initialization","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2014\/11\/25\/strange-tales-of-swift-initialization\/","title":{"rendered":"Strange Tales of Swift Initialization"},"content":{"rendered":"<p><a href=\"http:\/\/www.russbishop.net\/swift-and-uitableviewcontroller\">Russ Bishop<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.russbishop.net\/swift-and-uitableviewcontroller\"><p>Swift <em>appears<\/em> to treat initialization quite differently than Objective-C, even though under the hood it is actually quite similar. They both use two-phase initialization, but Objective-C simply hides the first phase from you.<\/p>\n<p>[&#8230;]<\/p>\n<p>Objective-C slaps some zeros on it and calls it done. Swift has no choice but to offer you access to this initialization phase. And by <em>offer<\/em> I mean <em>make you an offer you can&rsquo;t refuse<\/em>.<\/p>\n<p>[&#8230;]<\/p>\n<p>Once Swift has walked the class chain setting all properties to appropriate default values, even including <em>partially executing<\/em> your initializers up until the point where they call <code>super<\/code>, it then resumes executing initialization code starting from the most-derived superclass as it walks back up the chain.<\/p>\n<p>[&#8230;]<\/p>\n<p>If you have a funny suspicious feeling in the back of your mind right now congrats: you&rsquo;re a seasoned Objective-C pro. <em>A lot of classes in Objective-C don&rsquo;t follow the proper rules for initialization and\/or have incorrectly marked headers<\/em>. This includes plenty of Apple&rsquo;s own classes.<\/p><\/blockquote>\n<p>In other words, Swift&rsquo;s strictness is causing crashes where there were none before, but this is only because of latent bugs in the frameworks. However, Swift&rsquo;s initializer rules are also causing <a href=\"https:\/\/twitter.com\/mpweiher\/status\/535757306613743616\">headaches<\/a> and badly structured code.<\/p>","protected":false},"excerpt":{"rendered":"<p>Russ Bishop: Swift appears to treat initialization quite differently than Objective-C, even though under the hood it is actually quite similar. They both use two-phase initialization, but Objective-C simply hides the first phase from you. [&#8230;] Objective-C slaps some zeros on it and calls it done. Swift has no choice but to offer you access [&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":[46,54,71,901],"class_list":["post-10151","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-objective-c","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10151","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=10151"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10151\/revisions"}],"predecessor-version":[{"id":10152,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10151\/revisions\/10152"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=10151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=10151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=10151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}