swift enum mirror get associated value name

1.1k Views Asked by At

I need to get the name of enum associated value.

for example:

enum App{
    case iOS(version:String)
    case android(version:String, build:Int)
}
let iosApp = App.iOS(version:"2.30.11")
let androidApp = App.android(version:"2.30.11",build:101)
let iosMirror = Mirror(reflecting: iosApp)
for case let (key?, value) in iosMirror.children {
        print("\(key)-\(value)") //this will print:iOS-2.30.11,missing the value name-"version",the string "version" was I need.
}
let androidMirror = Mirror(reflecting: androidApp)
for case let (key?, value) in androidMirror.children {
        print("\(key)-\(value)") //this will print:android-(version:"2.30.11",build:101)
}

question: I want get the associated value name "version" of iosApp from iosMirror,How should i do? or using other way(not Mirror) to get strings "version".

1

There are 1 best solutions below

0
On

iOS-(version: "2.30.11") android-(version: "2.30.11", build: 101)gets printed with Xcode10(Swift4.2) so it is a bug from an older version.

-@Purpose

I tested this code in Xcode 10-beta 6,it print iOS-(version: "2.30.11"). this question solved.

thanks @Purpose.