In my custom APIClient Swift class, I defined an helper method that extract the value of a given key from a response dictionary, check its type, and throw an error when the value does not correspond to the type I want.
If I allow nil values, I just type String?.self instead of String.self in the method call
static func value<T>(forKey key: String, in dict: [String:Any], type: T.Type) throws -> T? {
let required = !(T.self is ExpressibleByNilLiteral.Type)
let val = dict[key]
if val == nil {
if required {
throw APIClientResponseError.missing(key: key, in: dict)
}
return nil
}
if !(val is T) {
throw APIClientResponseError.invalid(key: key, in: dict)
}
return val as? T
}
But I need to keep the T? return type otherwise I'm not able to return nil values.
How can I automate the output type based on type parameter? So it can output nil values when giving an optional type, and non-nil values when giving a required type.
Thanks!
I would just write this as two functions. It is much simpler. The caller still can choose whether the value is "required" not by passing in an optional type, but by calling the appropriate function.
If you really want one function, you can abstract the "return nil or throw error" logic into a protocol. Though to be honest, this is more like a hack than anything I would use in production.