Question:

Once this code is executed, how many items will be in the result array?

let names: [String?] = ["Barbara", nil, "Janet", nil, "Peter", nil, "George"]
let result = names.flatMap { $0 }

Correct answer: 4.

Explanation: Swift's flatMap will automatically strip nil values from an array, meaning that result will contain the names Barbara, Janet, Peter, and George.

 


Keywords:

© 2017 QuizBucket.org