What is dictionary.customMirror in Swift?

Overview

The customMirror property of a dictionary returns a Mirror that reflects that dictionary. A Mirror in Swift is used to show the parts that make up a particular object. The object could be a string, double, or dictionary value. The parts it will show can be the object’s stored properties, collection, and so on.

Syntax

dictionary.customMirror

Return property

The property returned is the Mirror for the dictionary (for example, dict).

Code example

// create a dictionary
let dict = [
"a" : "Apple",
"b" : "Beta",
"c" : "coding",
"d" : "developer",
"e" : "edpresso"
]
// get the mirror
print(dict.customMirror)

Code explanation

  • Lines 2–8: We created a dictionary dict. Then, we initialize it with some key-value pairs.

  • Line 11: We got the Mirror of the dictionary using the customMirror and print the result to the console.