MirrorType
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’s tools enable the immediate feedback and sense of exploration offered by Xcode Playgrounds.
[…]
The entry point for reflection is the
reflect
function, which can take an instance of any type as its single parameter and returns aMirrorType
. Now,MirrorType
is something of an oddity for the Swift standard library: a protocol used as a type. Other than the ubiquitousAnyObject
, to date no other protocol is used this way. The particularMirrorType
-conforming instance that you receive depends on the type passed toreflect
—Swift’s internals define mirrors for types such asArray
,Dictionary
,Optional
, andRange
, along with more generic mirrors for structs, classes, tuples, and metatypes.
MirrorType
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.[…]
Lastly, we must link
WWDCSession
to its custom mirror by adding conformance to theReflectable
protocol. Conformance only requires a single new method,getMirror()
, which returns aMirrorType
—in this case, our shiny newWWDCSessionMirror
:
Update (2015-12-20): Benedikt Terhechte:
So, as you can see, using reflection slows the whole process of creating
NSManagedObjects
down by about 3.5x. This is fine when you’re using this for a limited amount of items, or when you don’t have to care about speed. However, when you need to reflect over a huge amount ofstructs
, this will probably kill your app’s performance.