iOS ● ● ○ Swift 6

Kodebits Day 71: Value vs Reference

Aug 7 2026
Practice value semantics with a short swift challenge.

What does this print?

struct Val { var n: Int }
class Ref {
  var n: Int
  init(_ n: Int) { self.n = n }
}
var v1 = Val(n: 3)
var v2 = v1
v2.n = 7
var r1 = Ref(5)
var r2 = r1
r2.n = 9
print("\(v1.n) \(r1.n)")


Try it in the online Swift Playground →

[spoiler title="Solution"]

Answer:

3 9

Explanation:

Structs copy on assignment; classes share references.

[/spoiler]


Further Reading