# Using Reflections API (Mirror) with SwiftUI?

**URL:** <https://forums.swift.org/t/using-reflections-api-mirror-with-swiftui/43853>\
**Category:** Using Swift\
**Tags:** swiftui\
**Created:** [January 15, 2021, 1:59am UTC](https://forums.swift.org/t/using-reflections-api-mirror-with-swiftui/43853 "2021-01-15T01:59:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nogi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nogi/32/14899_2.png) [@nogi](https://forums.swift.org/u/nogi)\
**Post date:** [January 15, 2021, 1:59am UTC](https://forums.swift.org/t/using-reflections-api-mirror-with-swiftui/43853/1 "2021-01-15T01:59:26Z")

</div>

Hi,

I am new to SwiftUI and am wanting to iterate over a class and present in a view the keyname and keyvalue. I understand from googling that I need to use something like the reflections API to achieve this but am struggling to convert the swift example to work with swiftui.

My current non-working code:

```
import SwiftUI

struct ShowClassData: View {
    @ObservedObject var myClass : MyClass
    
    var body: some View {
        VStack{
            var mirror = Mirror(reflecting: myClass)
            
            mirror.children.forEach({
                Text("\(child.label): \(child.value)")
            })
        }
    }
}

```

Get this error:

- Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols

I've also tried the following with no luck:

```
for case let (label?, value) in mirror.children {
    print (label, value)
}

```

Different error:

- Closure containing control flow statement cannot be used with function builder 'ViewBuilder'

---

<div class="post-metadata">

**Author:** ![Max\_Desiatov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/max_desiatov/32/20654_2.png) [@Max\_Desiatov](https://forums.swift.org/u/Max_Desiatov)\
**Post date:** [January 15, 2021, 8:58am UTC](https://forums.swift.org/t/using-reflections-api-mirror-with-swiftui/43853/2 "2021-01-15T08:58:23Z")

</div>

The problem is that `mirror.children.forEach` does not return anything that conform to the SwiftUI `View` protocol, in fact it doesn't return anything as `forEach` is a void-returning function. You should use `ForEach` instead:

```swift
struct ShowClassData: View {
    @ObservedObject var myClass : MyClass

    var body: some View {
        VStack {
            let mirror = Mirror(reflecting: myClass)
            ForEach(Array(mirror.children), id: \.label) { child in
                Text("\(child.label!): \(String(describing: child.value))")
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![nogi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nogi/32/14899_2.png) [@nogi](https://forums.swift.org/u/nogi)\
**Post date:** [January 15, 2021, 9:35am UTC](https://forums.swift.org/t/using-reflections-api-mirror-with-swiftui/43853/3 "2021-01-15T09:35:33Z")

</div>

> [@Max\_Desiatov](#):
>
> ```swift
> var body: some View {
> VStack {
> let mirror = Mirror(reflecting: myClass)
> ForEach(Array(mirror.children), id: \.label) { child in
> Text("\(child.label!): \(String(describing: child.value))")
> }
> }
> }
> }
> 
> ```

Oh that's awesome thank you. Now, all I need to do is clean that output up as it's not very user friendly.

`[(label: Optional("_varA1_completeDate"), value: Combine.Published<Swift.Optional<Foundation.Date>>(storage: Combine.Published<Swift.Optional<Foundation.Date>>.Storage.value(Optional(2021-01-15 09:41:07 +0000)))), (label: Optional("_varA2_images"), value: Combine.Published<Swift.Array<SwiftUI.Image>>(storage: Combine.Published<Swift.Array<SwiftUI.Image>>.Storage.value([SwiftUI.Image(provider: SwiftUI.ImageProviderBox<__C.UIImage>)]))), (label: Optional("_varA2_completeDate"), value: Combine.Published<Swift.Optional<Foundation.Date>>(storage: Combine.Published<Swift.Optional<Foundation.Date>>.Storage.value(Optional(2021-01-15 09:41:12 +0000)))), (label: Optional("_varA2_switchesOff"), value: Combine.Published<Swift.Bool>(storage: Combine.Published<Swift.Bool>.Storage.value(true))), (label: Optional("_varA3_completeDate"), value: Combine.Published<Swift.Optional<Foundation.Date>>(storage: Combine.Published<Swift.Optional<Foundation.Date>>.Storage.value(nil))), (label: Optional("_varA4_completeDate"), value: Combine.Published<Swift.Optional<Foundation.Date>>(storage: Combine.Published<Swift.Optional<Foundation.Date>>.Storage.value(Optional(2021-01-15 09:41:13 +0000)))), (label: Optional("_varA5_completeDate"), value: Combine.Published<Swift.Optional<Foundation.Date>>(storage: Combine.Published<Swift.Optional<Foundation.Date>>.Storage.value(Optional(2021-01-15 09:41:14 +0000)))), (label: Optional("_varA6_completeDate"), value: Combine.Published<Swift.Optional<Foundation.Date>>(storage: Combine.Published<Swift.Optional<Foundation.Date>>.Storage.value(Optional(2021-01-15 09:41:15 +0000))))]`
