mardi 2 avril 2019

How to extract `rawValue` from `Any` if value is `RawRepresentable`

I'm looking for something like:

func extractRawValue(fromPossibleRawRepresentable value: Any) -> Any? {
    return (value as? RawRepresentable)?.rawValue
}

I don't mind if extracted RawValue needs to be static type...

func extractRawValue<T: RawRepresentable, U>(fromPossibleRawRepresentable value: Any, expecting: U.Type) -> U? where T.RawValue == U {
    return (value as? T)?.rawValue
}

Context: I want to collect rather raw than actual values in mirror.

let d = Mirror(reflecting: self).children.reduce(into: [String: String](), {
    guard let label = $1.label else {
        return
    }
    $0[label] = extractRawValue(fromPossibleRawRepresentable: $1.value)
}





Aucun commentaire:

Enregistrer un commentaire