我需要获取枚举关联值的名称。
例如:
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)
}
问题:我想从iosMirror获取iosApp的关联值名称“version”,该怎么做?或使用其他方式(不是镜像)来获取字符串“版本”。