Reputation: 8091
I have a struct (is a SwiftUI - View in this case, but the question has nothing to do with SwiftUI because it could be any Struct) with a generic which should be an Enum - type. Can i ask somehow the generic, what type the enum is?
example:
enum TestA : String {
case a = "a"
case b = "b"
}
var id : TestA
so i would like to have something like
if id.isKind(of:String)
as it works for classes.
import SwiftUI
struct Whatever<EnumType>: View {
let id: EnumType
func askForType() {
if id.isKind(of:String) { <<< this is not working, i know, but is there a workaround?
print("is string")
}
}
var body: some View {
Text("aha")
}
}
Upvotes: 0
Views: 115
Reputation: 1956
I found a good work around but might need some more logic.
In SwiftUI,
enum TestA : Int {
case a = 1
case b = 2
}
struct ContentView: View {
let id = TestA.self
func askForType() -> String{
return String(describing: type(of: id.RawValue))
}
var body: some View {
Text(askForType())
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
What this does is print the demangled type of the enum. In the example, this prints Int.rawvalue
. Removing .rawValue
from the type will only print the Enum identifier rather than type.
Upvotes: 2