{"id":10658,"date":"2015-02-01T19:22:16","date_gmt":"2015-02-02T00:22:16","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=10658"},"modified":"2015-02-01T19:22:16","modified_gmt":"2015-02-02T00:22:16","slug":"dynamic-casting-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/02\/01\/dynamic-casting-in-swift\/","title":{"rendered":"Dynamic Casting in Swift"},"content":{"rendered":"<p><a href=\"http:\/\/blog.segiddins.me\/2015\/01\/25\/dynamic-casting-in-swift\/\">Samuel E. Giddins<\/a>:<\/p>\n<blockquote cite=\"http:\/\/blog.segiddins.me\/2015\/01\/25\/dynamic-casting-in-swift\/\">\n<p>We can even define some syntactic sugar on top of this type verification:<\/p>\n<pre>func id&lt;U&gt;(object: AnyObject) -&gt; U? {\n  if let typed = object as? U {\n    return typed\n  }\n  return nil\n}\n<\/pre>\n<p>[&#8230;]<\/p>\n<p>OK, that last example might not look like a huge improvement, but coupled with Swift&rsquo;s powerful type inference, you can 'magically&rsquo; <code>type<\/code> objects at runtime with a single function call.<\/p>\n<\/blockquote>\n<p>You have to call the <code>id()<\/code> function, but depending on the context Swift may be able to infer the types.<\/p>","protected":false},"excerpt":{"rendered":"<p>Samuel E. Giddins: We can even define some syntactic sugar on top of this type verification: func id&lt;U&gt;(object: AnyObject) -&gt; U? { if let typed = object as? U { return typed } return nil } [&#8230;] OK, that last example might not look like a huge improvement, but coupled with Swift&rsquo;s powerful type inference, [&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,71,901],"class_list":["post-10658","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\/10658","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=10658"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10658\/revisions"}],"predecessor-version":[{"id":10659,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10658\/revisions\/10659"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=10658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=10658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=10658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}