Let's say I want to write a method on Arraythat either returns a copy of an array if its type is not optional, or a subarray of expanded values ββif its type is optional. To do this, I think I need to check if the array type is an Toptional type or not. For example, this simply returns a copy of the array anyway:
extension Array {
func unwrapped() -> Array<T> {
return filter({
var x: T? = $0
return x != nil
})
}
}
I understand that if I know that I have an array of options, I could just use filter () and map ():
let foo: [String?] = [nil, "bar", "baz"]
let bar: [String] = foo.filter({ $0 != nil }).map({ $0! })
I am not looking for a solution to this particular problem. I rather wonder if there is a way to determine in the extension of the array if its type is optional, which may be useful in several different convenience methods.