Michele Volpato

Michele Volpato

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.

Get a weekly email about Flutter

Subscribe to get a weekly curated list of articles and videos about Flutter and Dart.

    We respect your privacy. Unsubscribe at any time.

    Leave a comment