{"id":11411,"date":"2015-06-04T13:46:14","date_gmt":"2015-06-04T17:46:14","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=11411"},"modified":"2015-12-20T14:31:16","modified_gmt":"2015-12-20T19:31:16","slug":"mirrortype","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/06\/04\/mirrortype\/","title":{"rendered":"MirrorType"},"content":{"rendered":"<p><a href=\"http:\/\/nshipster.com\/mirrortype\/\">Nate Cook<\/a>:<\/p>\r\n<blockquote cite=\"http:\/\/nshipster.com\/mirrortype\/\">\r\n<p>Reflection in Swift is a limited affair, providing read-only access to a subset of type metadata. While far from the rich array of run-time hackery familiar to seasoned Objective-C developers, Swift&rsquo;s tools enable the immediate feedback and sense of exploration offered by Xcode Playgrounds.<\/p>\r\n<p>[&#8230;]<\/p>\r\n<p>The entry point for reflection is the <code>reflect<\/code> function, which can take an instance of any type as its single parameter and returns a <code>MirrorType<\/code>. Now, <code>MirrorType<\/code> is something of an oddity for the Swift standard library: a protocol used as a type. Other than the ubiquitous <code>AnyObject<\/code>, to date no other protocol is used this way. The particular <code>MirrorType<\/code>-conforming instance that you receive depends on the type passed to <code>reflect<\/code>&mdash;Swift&rsquo;s internals define mirrors for types such as <code>Array<\/code>, <code>Dictionary<\/code>, <code>Optional<\/code>, and <code>Range<\/code>, along with more generic mirrors for structs, classes, tuples, and metatypes.<\/p>\r\n<p><code>MirrorType<\/code> provides the nascent reflection API that Swift offers, wrapping a value along with its type information, information about its children, and different representations of the instance.<\/p>\r\n<p>[&#8230;]<\/p>\r\n<p>Lastly, we must link <code>WWDCSession<\/code> to its custom mirror by adding conformance to the <code>Reflectable<\/code> protocol. Conformance only requires a single new method, <code>getMirror()<\/code>, which returns a <code>MirrorType<\/code>&mdash;in this case, our shiny new <code>WWDCSessionMirror<\/code>:<\/p>\r\n<\/blockquote>\r\n\r\n<p>Update (2015-12-20): <a href=\"http:\/\/appventure.me\/2015\/10\/24\/swift-reflection-api-what-you-can-do\/\">Benedikt Terhechte<\/a>:<\/p>\r\n<blockquote cite=\"http:\/\/appventure.me\/2015\/10\/24\/swift-reflection-api-what-you-can-do\/\">\r\n<p>So, as you can see, using reflection slows the whole process of creating <code>NSManagedObjects<\/code> down by about <b>3.5x<\/b>. This is fine when you\u2019re using this for a limited amount of items, or when you don\u2019t have to care about speed. However, when you need to reflect over a huge amount of <code>structs<\/code>, this will probably kill your app\u2019s performance.<\/p>\r\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Nate Cook: Reflection in Swift is a limited affair, providing read-only access to a subset of type metadata. While far from the rich array of run-time hackery familiar to seasoned Objective-C developers, Swift&rsquo;s tools enable the immediate feedback and sense of exploration offered by Xcode Playgrounds. [&#8230;] The entry point for reflection is the reflect [&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-11411","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\/11411","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=11411"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11411\/revisions"}],"predecessor-version":[{"id":13159,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11411\/revisions\/13159"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=11411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=11411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=11411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}