A representation of the substructure and display style of an instance of any type.


struct Mirror


A mirror describes the parts that make up a particular instance, such as the instance’s stored properties, collection or tuple elements, or its active enumeration case. Mirrors also provide a “display style” property that suggests how this mirror might be rendered.

Playgrounds and the debugger use the Mirror type to display representations of values of any type. For example, when you pass an instance to the dump(_:_:_:_:) function, a mirror is used to render that instance’s runtime contents.

struct Point {
    let x: Int, y: Int

let p = Point(x: 21, y: 30)
print(String(reflecting: p))
// Prints "▿ Point
//           - x: 21
//           - y: 30"

To customize the mirror representation of a custom type, add conformance to the CustomReflectable protocol.


Querying Descendants

func descendant(MirrorPath, MirrorPath...) -> Any?

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

protocol MirrorPath

A protocol for legitimate arguments to Mirror’s descendant method.

Type Aliases

typealias Mirror.Child

An element of the reflected instance’s structure.

typealias Mirror.Children

The type used to represent substructure.


init(reflecting: Any)

Creates a mirror that reflects on the given instance.

Instance Properties

let children: Mirror.Children

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

var customMirror: Mirror

The custom mirror for this instance.

var description: String

A textual representation of this instance.

let displayStyle: Mirror.DisplayStyle?

A suggested display style for the reflected subject.

let subjectType: Any.Type

The static type of the subject being reflected.

var superclassMirror: Mirror?

A mirror of the subject’s superclass, if one exists.


enum Mirror.AncestorRepresentation

The representation to use for ancestor classes.

enum Mirror.DisplayStyle

A suggestion of how a mirror’s subject is to be interpreted.


See Also

Querying Runtime Values

struct ObjectIdentifier

A unique identifier for a class instance or metatype.

func type<T, Metatype>(of: T) -> Metatype

Returns the dynamic type of a value.

func == (Any.Type?, Any.Type?) -> Bool

Returns a Boolean value indicating whether two types are identical.

func != (Any.Type?, Any.Type?) -> Bool

Returns a Boolean value indicating whether two types are not identical.