Activity 1
Create a measured value
Define a Rectangle with width and height stored properties, a computed area, and an initializer that prevents negative dimensions.
Hint: Decide whether to clamp invalid values or make the initializer failable.
Swift Fundamentals
Build well-formed Swift types with stored state, derived values, behavior, and safe initialization.
Before you begin
Learning stage
Properties store or compute information that belongs to a type. Stored properties keep values, while computed properties derive a value through a getter and an optional setter without adding separate storage.
Methods define behavior. A structure method that changes stored properties must be marked mutating. Initializers prepare a new instance and must ensure that every non-optional stored property has a value before initialization completes.
Learning stage
This value type owns stored progress, derives a percentage, and protects mutation behind a method.
struct CourseProgress {
let totalLessons: Int
private(set) var completedLessons: Int
var percentage: Double {
Double(completedLessons) / Double(totalLessons) * 100
}
init(totalLessons: Int) {
self.totalLessons = max(totalLessons, 1)
completedLessons = 0
}
mutating func completeNextLesson() {
guard completedLessons < totalLessons else { return }
completedLessons += 1
}
}
var progress = CourseProgress(totalLessons: 14)
progress.completeNextLesson()
print("Progress: \(progress.percentage)%")Learning stage
These concepts appear throughout production iOS code. Focus on the requirement each technique communicates.
Learning stage
Notice these mistakes early so the compiler and type system can help instead of becoming obstacles.
Learning stage
Work through these activities in a small Xcode project. No embedded playground is required on this site.
Activity 1
Define a Rectangle with width and height stored properties, a computed area, and an initializer that prevents negative dimensions.
Hint: Decide whether to clamp invalid values or make the initializer failable.
Activity 2
Create a StudyStreak type whose count can be read publicly but changed only through increment and reset methods.
Hint: private(set) can restrict direct writes.
Learning stage
Answer each question before revealing the explanation.
QUESTION 1
No. It calculates and returns a value when accessed.
QUESTION 2
Every stored property must have a valid initialized value, unless it already has a default or is an optional initialized to nil.
Learning stage
Explain the idea in terms of its purpose, behavior, and tradeoffs rather than reciting syntax.
Short answer: Stored properties keep data; computed properties calculate a value through accessors.
A stored property contributes to an instance's stored state. A computed property defines a getter and optionally a setter, deriving its result from other information. Computed properties are useful when storing the same information separately would create synchronization problems.
Possible follow-ups
Learning stage
A useful type protects a valid state and gives its behavior meaningful names. Initialization establishes the state; properties expose information; methods coordinate change.