CompactMap for optional unwrapping
📖 Scenario: You have a list of strings representing ages of people. Some strings are valid numbers, and some are not. You want to get a list of only the valid ages as integers.
🎯 Goal: Build a Swift program that uses compactMap to convert an array of optional strings into an array of non-optional integers, filtering out invalid entries.
📋 What You'll Learn
Create an array of strings called
ageStrings with the exact values: ["25", "thirty", "42", "", "18"]Create a variable called
validAges that uses compactMap on ageStrings to convert strings to IntPrint the
validAges array💡 Why This Matters
🌍 Real World
Filtering and converting user input or data from text sources where some values may be invalid or missing.
💼 Career
Data cleaning and transformation is a common task in app development, especially when handling user input or external data.
Progress0 / 4 steps