Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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].