Type aliases for readability
📖 Scenario: You are working on a simple app that tracks distances in kilometers and miles. To keep your code clear and easy to understand, you want to use type aliases to give meaningful names to the distance values.
🎯 Goal: Create type aliases for Kilometers and Miles as Double. Then use these aliases to declare variables and convert between kilometers and miles.
📋 What You'll Learn
Create a type alias called
Kilometers for DoubleCreate a type alias called
Miles for DoubleDeclare a variable
distanceInKm of type Kilometers with value 5.0Declare a variable
distanceInMiles of type Miles and convert distanceInKm to miles using the formula miles = kilometers * 0.621371Print the value of
distanceInMiles with a descriptive message💡 Why This Matters
🌍 Real World
Type aliases are useful in real apps to make code clearer, especially when working with units like distances, times, or currencies.
💼 Career
Understanding type aliases helps you write readable and maintainable Swift code, a skill valued in iOS and macOS development jobs.
Progress0 / 4 steps