Files
swiftpm-mustache/Sources/HummingbirdMustache/Mirror.swift
2021-03-12 09:55:12 +00:00

18 lines
478 B
Swift

func unwrapOptional(_ object: Any) -> Any? {
let mirror = Mirror(reflecting: object)
guard mirror.displayStyle == .optional else { return object }
guard let first = mirror.children.first else { return nil }
return first.value
}
extension Mirror {
func getValue(forKey key: String) -> Any? {
guard let matched = children.filter({ $0.label == key }).first else {
return nil
}
return unwrapOptional(matched.value)
}
}