{"id":10833,"date":"2015-03-14T11:21:56","date_gmt":"2015-03-14T15:21:56","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=10833"},"modified":"2015-03-14T11:21:56","modified_gmt":"2015-03-14T15:21:56","slug":"simple-reflection-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/03\/14\/simple-reflection-in-swift\/","title":{"rendered":"Simple Reflection in Swift"},"content":{"rendered":"<p><a href=\"http:\/\/freecake.yayuhh.com\/simple-reflection-in-swift\/\">Angelo Di Paolo<\/a> (via <a href=\"https:\/\/twitter.com\/getaaron\/status\/574939196466462722\">Aaron Brager<\/a>):<\/p>\n<blockquote cite=\"http:\/\/freecake.yayuhh.com\/simple-reflection-in-swift\/\">\n<p>Swift offers <em>metatype type<\/em> to refer to the type of any Swift type such as enumerations, structures, and protocols. To access the value of the metatype type use the postfix <code>self<\/code> expression. [&#8230;] Now the value of <code>objectType<\/code> can be used to create a new <code>SomeClass<\/code> instance just like how a <code>Class<\/code> value would be used in Objective-C.<\/p>\n<p>[&#8230;]<\/p>\n<p>If you examine Swift in Xcode you may have noticed the <code>Reflectable<\/code> and <code>MirrorType<\/code> protocols. [&#8230;] This mirror-based reflection API is not yet officially documented anywhere (at least that I know of) and it seems that for now it is mostly used by the REPL. However, this limited API does give us the capability to inspect some basic information about our values.<\/p>\n<p>[&#8230;]<\/p>\n<p>The Swift standard library defines a <code>reflect<\/code> method that takes a value of any type and returns a mirror (<code>MirrorType<\/code>) object which provides information about the value such as its type and properties.<\/p>\n<p>[&#8230;]<\/p>\n<p>You may have noticed that our <code>Brew<\/code> struct does not conform to <code>Reflectable<\/code> and despite the lack of a <code>getMirror()<\/code> implementation we are still able to produce a mirror when calling <code>reflect(brew)<\/code>. This works because the runtime provides a fall back <code>MirrorType<\/code> implementation that works for any type when a mirror is not directly provided for a type.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Angelo Di Paolo (via Aaron Brager): Swift offers metatype type to refer to the type of any Swift type such as enumerations, structures, and protocols. To access the value of the metatype type use the postfix self expression. [&#8230;] Now the value of objectType can be used to create a new SomeClass instance just like [&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-10833","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\/10833","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=10833"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10833\/revisions"}],"predecessor-version":[{"id":10834,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10833\/revisions\/10834"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=10833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=10833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=10833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}