Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Trevor Duersch
Trevor Duersch
9,964 Points

Is flatMap deprecated?

Is flatMap depcreated? I am currently in the video on closures and reviewing the "Flat Map" video - https://teamtreehouse.com/library/flat-map . I got a warning saying that flatMap was deprecated and I should use compactMap. Once I changed the code from "flatMap" to "compactMap", the functionality was still there and the warning went away.

2 Answers

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Correct. In essence, flatMap was just renamed to compactMap because it makes more sense. From this method, you only get the non-nil results of the closure passed in. nil is left out of the resulting array.

@Caleb : not really the same result, it doesn't flatten the array of array to an array. In the video example, the type of tags is :

  • [[String]] when using compactMap
  • [String] when using flatMap
Caleb Kleveter
Caleb Kleveter
Treehouse Moderator 37,862 Points

flatMap is deprecated for operations that returned an optional. Previously you could use .flatMap and if the closure returned an optional, any nil values would not appear in the resulting array. This was simply renamed to compactMap.

In the case that you are talking about, that is correct, you use flatMap to convert [[Element]] to [Element].