Adding computed properties
📖 Scenario: You are creating a simple app to manage rectangles. Each rectangle has a width and height. You want to add a property that calculates the area automatically.
🎯 Goal: Build a Swift struct called Rectangle with stored properties for width and height, and add a computed property called area that calculates the rectangle's area.
📋 What You'll Learn
Create a struct named
Rectangle with two stored properties: width and height of type Double.Add a computed property called
area that returns the product of width and height.Create an instance of
Rectangle with width 5.0 and height 3.0.Print the value of the
area property.💡 Why This Matters
🌍 Real World
Computed properties help keep data consistent and avoid manual calculations in apps that manage shapes, sizes, or any related data.
💼 Career
Understanding computed properties is essential for Swift developers building iOS apps, as it helps write clean, efficient, and maintainable code.
Progress0 / 4 steps