1 min read

Remove nil elements from an array while changing the array type

I always forget how to remove nil elements from an array in a functional way in Swift.

Here it is:

let arrayWthOptionals: [String?] = ...
let arrayWithoutOptionals: [String] = arrayWthOptionals.compactMap ({ $0 })

The resulting array is an array of non-optional.

Wanna stay up-to-date with Flutter and Dart?

Subscribe to get a weekly email with the best articles about Flutter and Dart.

    We won't send you spam. Unsubscribe at any time.