【发布时间】:2016-11-10 19:26:50
【问题描述】:
所以我有一个接收Any 的函数,它使用反射检查 Any 是否为枚举:
func extractRawValue(subject: Any) throws -> Any {
let mirror = Mirror(reflecting: subject)
guard let displayStyle = mirror.displayStyle,
case .`enum` = displayStyle else {
throw Errors.NoEnum
}
// And from here I don't know how to go any further...
// I wish I could do something like this:
guard let subject = subject as? RawRepresentable where let rawValue = subject.rawValue as Any else {
throw Errors.NoRawRepresenable
}
return rawValue
}
有谁知道我怎样才能完成这样的事情?
【问题讨论】:
-
guard let subject = subject as? RawRepresentable where let rawValue = subject.rawValue as Any想问什么问题?我看到第一部分询问这个东西是否是一个 RawRepresentable,但我无法想象第二部分应该是什么意思。在这里帮帮我。 -
你成功了吗?我试图在这里找到类似的东西:stackoverflow.com/questions/43666118/…
标签: swift reflection enums rawrepresentable