O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Removing nil values from an array

We can use flatMap to get values out of optionalArray and remove nil values:

let optionalArray: [String?] = ["First", "Second", nil, "Fourth"] let nonOptionalArray = optionalArray.flatMap { $0 } print(nonOptionalArray) 

The result will be ["First", "Second", "Fourth"], as expected.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required