3

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
    I am not sure if that is possible, because Swift does not distinguish between single-element tuples and their (only) element. – Martin R Sep 02 '18 at 15:01
  • Thanks,Is other way exist(not using Mirror reflect) to get string "version"? – user2967434 Sep 02 '18 at 15:06
  • `iOS-(version: "2.30.11") android-(version: "2.30.11", build: 101)`gets printed with Xcode10(Swift4.2) so it is probably a bug from an older version. – Fabian Sep 02 '18 at 15:23

1 Answers1

0

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.