map()
and compactMap()
?Suggested approach: Remember to give practical examples as well as outlining the core differences. So, you might start by saying the map()
transforms a sequence using a function we specify, whereas compactMap()
does that same step but then unwraps its optionals and discards any nil values. For example, converting an array of strings into integers works better with compactMap()
, because creating an Int
from a String
is failable.
Estimated difficulty: Easy
Watch me answer this question in detail
See the full list of iOS interview questions
Link copied to your pasteboard.