func flatten(_ opt: Any?) -> Any? {
guard let unwrapped = opt else {
return nil
}
if Mirror(reflecting: unwrapped).displayStyle == .optional {
return flatten(Mirror(reflecting: unwrapped).children.first!.value)
} else {
return Optional(unwrapped)
}
}
I think this will do it. I feel a bit dirty after writing this, but hey, it seems to do the job. Let me know if it does/doesn't work, it's late and I'm going to go to bed now. ![]()
I ended up using Mirror to avoid having to create a protocol, but a protocol-based solution may be more desirable if you want "vanilla" Swift code. Perhaps there exists a way to do this without either caveat!