Menu
CoddyTech

Structs and Independent Copies

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

C struct assignment copies member values, unlike assigning a pointer to the same struct. Swift structs also have value semantics and additionally support methods directly on the type. A Swift instance method that changes stored properties declares mutating and needs a mutable receiver.

Both examples change only the second theme value. The C struct stores pointers to string literals and replaces one pointer member; it does not modify or clone literal bytes. Swift stores String values and updates the second instance through its mutating method. The matching output comes from each representation preserving the first value.

C: complete program

theme.h

#ifndef THEME_H
#define THEME_H
typedef struct { const char *name; } Theme;
static void theme_select(Theme *theme, const char *name) { theme->name = name; }
#endif

main.c

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

int main(void) {
Theme first = {"light"};
    Theme second = first;
    theme_select(&second, "dark");
    printf("%s %s\n", first.name, second.name);
    return 0;
}

Swift: complete program

Theme.swift

import Foundation
struct Theme {
    var name: String
    mutating func select(_ value: String) { name = value }
}

main.swift

import Foundation
let first = Theme(name: "light")
var second = first
second.select("dark")
print("\(first.name) \(second.name)")

Pointer fields in a copied C struct can still share their pointed-to objects. Swift structs containing class references likewise do not recursively clone those instances.

challenge icon

Challenge

Easy

Implement value type Score with stored points: Int, init(points:), and mutating award(_ amount: Int) adding amount. Input is initial points and award, each 0 through 1000. Copy the original, award only the copy, and output original and copy points separated by a space.

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 Score.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 original = Score(points: 13); var copy = original; copy.award(6); copy.award(2); return original.points == 13 && copy.points == 21
}
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