Mirror

struct Mirror

Representation of the sub-structure and optional "display style" of any arbitrary subject instance.

Describes the parts---such as stored properties, collection elements, tuple elements, or the active enumeration case---that make up a particular instance. May also supply a "display style" property that suggests how this structure might be rendered.

Mirrors are used by playgrounds and the debugger.

Inheritance CustomReflectable, CustomStringConvertible View Protocol Hierarchy →
Associated Types
Child = (label: String?, value: Any)

An element of the reflected instance's structure. The optional label may be used when appropriate, e.g. to represent the name of a stored property or of an active enum case, and will be used for lookup when Strings are passed to the descendant method.

Children = AnyForwardCollection<Child>

The type used to represent sub-structure.

Depending on your needs, you may find it useful to "upgrade" instances of this type to AnyBidirectionalCollection or AnyRandomAccessCollection. For example, to display the last 20 children of a mirror if they can be accessed efficiently, you might write:

if let b = AnyBidirectionalCollection(someMirror.children) {
  for i in b.endIndex.advancedBy(-20, limit: b.startIndex)..<b.endIndex {
     print(b[i])
  }
}
Nested Types Mirror.AncestorRepresentation, Mirror.DisplayStyle
Import import Swift

Initializers

init<T>(_: T, children: DictionaryLiteral<String, Any>, displayStyle: Mirror.DisplayStyle?, ancestorRepresentation: Mirror.AncestorRepresentation)

Represent subject with labeled structure described by children, using an optional displayStyle.

Pass a dictionary literal with String keys as children. Be aware that although an actual Dictionary is arbitrarily-ordered, the ordering of the Mirror's children will exactly match that of the literal you pass.

If subject is not a class instance, ancestorRepresentation is ignored. Otherwise, ancestorRepresentation determines whether ancestor classes will be represented and whether their customMirror implementations will be used. By default, a representation is automatically generated and any customMirror implementation is bypassed. To prevent bypassing customized ancestors, customMirror overrides should initialize the Mirror with:

ancestorRepresentation: .Customized(super.customMirror)

Note: The resulting Mirror's children may be upgraded to AnyRandomAccessCollection later. See the failable initializers of AnyBidirectionalCollection and AnyRandomAccessCollection for details.

Declaration

init<T>(_ subject: T, children: DictionaryLiteral<String, Any>, displayStyle: Mirror.DisplayStyle? = default, ancestorRepresentation: Mirror.AncestorRepresentation = default)
init<T, C : CollectionType where C.Generator.Element == Child>(_: T, children: C, displayStyle: Mirror.DisplayStyle?, ancestorRepresentation: Mirror.AncestorRepresentation)

Represent subject with structure described by children, using an optional displayStyle.

If subject is not a class instance, ancestorRepresentation is ignored. Otherwise, ancestorRepresentation determines whether ancestor classes will be represented and whether their customMirror implementations will be used. By default, a representation is automatically generated and any customMirror implementation is bypassed. To prevent bypassing customized ancestors, customMirror overrides should initialize the Mirror with:

ancestorRepresentation: .Customized(super.customMirror)

Note: The traversal protocol modeled by children's indices (ForwardIndexType, BidirectionalIndexType, or RandomAccessIndexType) is captured so that the resulting Mirror's children may be upgraded later. See the failable initializers of AnyBidirectionalCollection and AnyRandomAccessCollection for details.

Declaration

init<T, C : CollectionType where C.Generator.Element == Child>(_ subject: T, children: C, displayStyle: Mirror.DisplayStyle? = default, ancestorRepresentation: Mirror.AncestorRepresentation = default)
init(_:unlabeledChildren:displayStyle:ancestorRepresentation:)

Represent subject with child values given by unlabeledChildren, using an optional displayStyle. The result's child labels will all be nil.

This initializer is especially useful for the mirrors of collections, e.g.:

extension MyArray : CustomReflectable {
  func customMirror() -> Mirror {
    return Mirror(self, unlabeledChildren: self, displayStyle: .Collection)
  }
}

If subject is not a class instance, ancestorRepresentation is ignored. Otherwise, ancestorRepresentation determines whether ancestor classes will be represented and whether their customMirror implementations will be used. By default, a representation is automatically generated and any customMirror implementation is bypassed. To prevent bypassing customized ancestors, customMirror overrides should initialize the Mirror with:

ancestorRepresentation: .Customized(super.customMirror)

Note: The traversal protocol modeled by children's indices (ForwardIndexType, BidirectionalIndexType, or RandomAccessIndexType) is captured so that the resulting Mirror's children may be upgraded later. See the failable initializers of AnyBidirectionalCollection and AnyRandomAccessCollection for details.

Declaration

init<T, C : CollectionType>(_ subject: T, unlabeledChildren: C, displayStyle: Mirror.DisplayStyle? = default, ancestorRepresentation: Mirror.AncestorRepresentation = default)
init(reflecting:)

Reflect upon the given subject.

If the dynamic type of subject conforms to CustomReflectable, the resulting mirror is determined by its customMirror method. Otherwise, the result is generated by the language.

Note: If the dynamic type of subject has value semantics, subsequent mutations of subject will not observable in Mirror. In general, though, the observability of such mutations is unspecified.

Declaration

init(reflecting subject: Any)

Instance Variables

var children: Children

A collection of Child elements describing the structure of the reflected subject.

Declaration

var children: Children { get }
var description: String

Declaration

var description: String { get }
var displayStyle: Mirror.DisplayStyle?

Suggests a display style for the reflected subject.

Declaration

var displayStyle: Mirror.DisplayStyle? { get }
var subjectType: Any.Type

The static type of the subject being reflected.

This type may differ from the subject's dynamic type when self is the superclassMirror() of another mirror.

Declaration

var subjectType: Any.Type { get }

Instance Methods

func customMirror()

Declaration

func customMirror() -> Mirror
func descendant(_:_:)

Returns a specific descendant of the reflected subject, or nil if no such descendant exists.

A String argument selects the first Child with a matching label. An integer argument n select the nth Child. For example:

var d = Mirror(reflecting: x).descendant(1, "two", 3)

is equivalent to:

var d = nil
let children = Mirror(reflecting: x).children
let p0 = children.startIndex.advancedBy(1, limit: children.endIndex)
if p0 != children.endIndex {
  let grandChildren = Mirror(reflecting: children[p0].value).children
  SeekTwo: for g in grandChildren {
    if g.label == "two" {
      let greatGrandChildren = Mirror(reflecting: g.value).children
      let p1 = greatGrandChildren.startIndex.advancedBy(3,
        limit: greatGrandChildren.endIndex)
      if p1 != endIndex { d = greatGrandChildren[p1].value }
      break SeekTwo
    }
  }

As you can see, complexity for each element of the argument list depends on the argument type and capabilities of the collection used to initialize the corresponding subject's parent's mirror. Each String argument results in a linear search. In short, this function is suitable for exploring the structure of a Mirror in a REPL or playground, but don't expect it to be efficient.

Declaration

func descendant(first: MirrorPathType, _ rest: MirrorPathType...) -> Any?
func superclassMirror()

Declaration

func superclassMirror() -> Mirror?