StructureSwift5.9.0
Mirror
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.
Citizens in Swift
Conformances
protocol CustomReflectable
A type that explicitly supplies its own mirror.
protocol CustomStringConvertible
A type with a customized textual representation.
Members
init<Subject, C>(Subject, children: C, displayStyle: Mirror.DisplayStyle?, ancestorRepresentation: Mirror.AncestorRepresentation
) Creates a mirror representing the given subject with a specified structure.
init<Subject>(Subject, children: KeyValuePairs<String, Any>, displayStyle: Mirror.DisplayStyle?, ancestorRepresentation: Mirror.AncestorRepresentation
) Creates a mirror representing the given subject using a dictionary literal for the structure.
init<Subject, C>(Subject, unlabeledChildren: C, displayStyle: Mirror.DisplayStyle?, ancestorRepresentation: Mirror.AncestorRepresentation
) Creates a mirror representing the given subject with unlabeled children.
init(reflecting: Any
) Creates a mirror that reflects on the given instance.
let children: Mirror.Children
A collection of
Child
elements describing the structure of the reflected subject.var customMirror: Mirror
var description: String
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.
func descendant(MirrorPath, MirrorPath...
) -> Any? Returns a specific descendant of the reflected subject, or
nil
if no such descendant exists.enum AncestorRepresentation
The representation to use for ancestor classes.
enum DisplayStyle
A suggestion of how a mirror’s subject is to be interpreted.
typealias Child
An element of the reflected instance’s structure.
typealias Children
The type used to represent substructure.