I was looking at ways to convert a Array<T?>
into an Array<T>?
based on whether or not any of the items are null. One of the options I considered for doing this when I couldn't figure out a short way to do it was to create an extension method on Array
for this, but I couldn't figure out how to do it.
How could I turn this method:
func toNonNullable<T>(array: [T?]) -> [T]? {
if array.filter({$0 == nil}).count != 0 {
return nil;
}
return array.map({$0!})
}
into an extension method on Array
?
I am not asking if it would be a good idea to add this extension method (I'm not convinced it is), just how I could do it (if it's possible at all).