Menu
CoddyTech

Access Control and Invariants

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

C struct fields are accessible wherever their definition is visible; exposing helper functions does not make those fields private. An opaque incomplete struct with a separate implementation can hide representation. Swift provides access control directly, including private(set) to expose reads while restricting writes.

The source uses conventional getter and mutation functions but deliberately shows the full struct, so callers could still modify its field. The Swift setter is restricted to its declaration and permitted same-file extensions. Swift internal means module visibility, not C external linkage or a header inclusion convention.

C: complete program

latch.h

#ifndef LATCH_H
#define LATCH_H
#include <stdbool.h>
typedef struct { bool open; } Latch;
static bool latch_is_open(const Latch *latch) { return latch->open; }
static void latch_close(Latch *latch) { latch->open = false; }
#endif

main.c

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "latch.h"

int main(void) {
Latch latch = {true};
    latch_close(&latch);
    puts(latch_is_open(&latch) ? "true" : "false");
    return 0;
}

Swift: complete program

Latch.swift

import Foundation
final class Latch {
    private(set) var isOpen = true
    func close() { isOpen = false }
}

main.swift

import Foundation
let latch = Latch()
latch.close()
print(latch.isOpen)

Read permission is not write permission. A subclass in another file does not automatically gain access to a superclass private setter; use its supported mutation API.

challenge icon

Challenge

Easy

Implement Wallet(initial:) with read-only-to-callers balance, and spend(_ amount: Int) -> Bool. Reject nonpositive or unaffordable spends without changing balance; otherwise subtract and return true. Initial balance is nonnegative. Input is initial balance 0 through 1000 followed by 0 through 30 spends -10 through 1000. Output final balance then one space then number of successful spends.

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 Wallet.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 {
let w = Wallet(initial: 17); guard !w.spend(18) && !w.spend(0) && !w.spend(-1) && w.balance == 17 else { return false }; return w.spend(7) && w.balance == 10 && w.spend(10) && w.balance == 0 && !w.spend(1)
}
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