---
title: "Swift mirroring"
slug: swift-mirroring
section: tech
date: 2016-12-03T21:20:00.000Z
canonical: https://roland.leth.ro/blog/tech/swift-mirroring
---

I [recently](https://github.com/rolandleth/LTHRadioButton/commit/05855513f4c9e704ff4b9b3104e4e7744d382087) added some tests to [LTHRadioButton](https://github.com/rolandleth/LTHRadioButton), and I gave [mirroring](https://developer.apple.com/reference/swift/mirror) a try, something I always wanted to do. What's mirroring? As Apple puts it, it's a *representation of the sub-structure and optional "display style" of any arbitrary subject instance*.

[Benedikt](https://appventure.me) has a really nice and comprehensive [post](https://appventure.me/2015/10/24/swift-reflection-api-what-you-can-do/) about it, so I'll just very shortly cover the children of a mirror. What are children? All the properties of an object, no matter if they are `public`, `internal`, or `private`. And this is where mirroring becomes interesting, and powerful: you can get access to the `private / internal` properties of an object - something I don't recommend on production code, unless **really** needed, and you're 110% sure of what you're doing. But for testing and debugging purposes, they're really, really great. One caveat, though, is that computed properties are not visible in mirrors.

Since LTHRadioButton is an open source library, I wanted to have proper access levels for its properties, but I also wanted to cover it with tests, which is why I turned to mirroring. Initializing one and accessing its children is pretty straightforward:

```swift
let radioButton = LTHRadioButton()
let mirror = Mirror(reflecting: radioButton)
mirror.children.forEach { child in
	print(child.label)
	print(child.value)
}
```

The `label` of a child is a `String?`, and it represents the property's name; the `value` of a child represents the object itself, and it's of type `Any`. The latter would then be optionally casted to the required type, and used as needed:

```swift
[...]
mirror.children.forEach { child in
	switch child.label {
	case "innerCircle"?:
		guard let innerCircle = child.value as? UIView else { return XCTFail() }
		XCTAssertEqual(	innerCircle.layer.borderColor, UIColor.blue.cgColor)
	[...]
	default: return
	}
}
```

There's much more to mirroring, like modifying  the properties of an object, for example, which is why I suggest you read Benedikt's post about it; I'm just scratching the surface here.