Sitemap
Cross Commit

Practical stories on Swift, SwiftUI, Kotlin, Jetpack Compose, mobile architecture, AI, and the latest tech, written by engineers who ship real apps. Open to writers worldwide.

Press enter or click to view image in full size

Member-only story

I Changed One Swift Variable. Why Did Another Value Change?

7 min readOct 28, 2026

--

The interviewer writes this Swift code on the screen:

final class Settings {
var theme = "Light"
}
struct UserProfile {
var name: String
var settings: Settings
}
var first = UserProfile(
name: "Alex",
settings: Settings()
)
var second = first
second.settings.theme = "Dark"
print(first.settings.theme)
print(second.settings.theme)

The candidate expects:

Light
Dark

But Swift prints:

Dark
Dark

The interviewer asks one question:

“You changed second. Why did first change too? Isn't UserProfile a struct?”

This is where a simple Swift interview question gets interesting.

friend link

The candidate knows that structures are value types. Assigning one structure to another should give you an independent value, right?

So why did changing one variable affect the other?

The answer reveals an important detail about Swift: a struct can have value semantics while still containing references to shared, mutable objects.

The First Mistake: Assuming a Struct Copies Everything

--

--

Kavya Sri
Kavya Sri

Written by Kavya Sri

Software engineer, writing down what I've actually learned along the way — about AI, careers, and the parts of this industry nobody explains to you upfront.

Cross Commit
Cross Commit

Published in Cross Commit

Practical stories on Swift, SwiftUI, Kotlin, Jetpack Compose, mobile architecture, AI, and the latest tech, written by engineers who ship real apps. Open to writers worldwide.