Menu
CoddyTech

Stored and Computed Properties

Lesson 27 of 48 in Coddy's Swift for C++ Developers course.

C++ commonly represents derived state through getter and setter functions, while Swift computed properties give those operations property syntax. The Swift minutes getter derives a result from seconds, and its setter maps newValue back into stored seconds. No independent minutes field is stored. The source uses explicit methods for the same whole-minute conversion. A getter is not automatically a cache, and a setter may have rounding or validation behavior that must be documented. Swift stored properties and property observers are different mechanisms. Keep initialization valid for the actual stored representation instead of updating unrelated copies of derived values.

C++

Complete program file: main.cpp.

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <optional>
#include <memory>
#include <tuple>
#include <variant>
#include <stdexcept>
#include <algorithm>
struct Duration {
    int seconds;
    int minutes() const { return seconds / 60; }
    void set_minutes(int value) { seconds = value * 60; }
};

int main() {
Duration duration{120};
    duration.set_minutes(3);
    std::cout << duration.seconds << '\n' << duration.minutes() << '\n';
}

Swift

Complete program file: Duration.swift.

struct Duration {
    var seconds: Int
    var minutes: Int {
        get { seconds / 60 }
        set { seconds = newValue * 60 }
    }
}

Complete program file: main.swift.

var duration = Duration(seconds: 120)
duration.minutes = 3
print(duration.seconds)
print(duration.minutes)
challenge icon

Challenge

Easy

Implement Rectangle with mutable integer width and height, memberwise initializer, and computed area: Int. Area is width times height and must reflect later mutations. Input is width, height, then replacement width, each 1 through 100. Output area before and after the replacement.

Implement the required APIs and solve(_ input: String) -> String. Return output text without a final newline; do not print in editable code. The locked driver independently checks the APIs on additional valid inputs, then prints the returned text with exactly one newline. All numeric operations stay within Int bounds.

Your code goes in Rectangle.swift, Functions.swift. main.swift holds the supplied input/output code and cannot be edited.

Try it yourself

import Foundation
func apiChecks(_ input: String) -> Bool {
var r = Rectangle(width: 6, height: 7); guard r.area == 42 else { return false }; r.height = 2; return r.area == 12
}
let input = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)!
// BEGIN PUBLIC API PROBES
let apiOK = apiChecks(input)
// END PUBLIC API PROBES
if apiOK { print(solve(input)) } else { print("API mismatch") }
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Swift for C++ Developers

Practice on your own: Swift playground