When you start building apps in Swift or SwiftUI, you’ll often hear the term “Copy-on-Write” — or CoW for short. It sounds fancy, but the idea is actually very simple and smart. Let’s break it down.
Swift uses value types like struct, Array, Dictionary, and String.
Normally, when you copy a value type, you’d expect it to create a new copy in memory. But copying big data every time can be slow and wasteful.
So Swift uses a trick called Copy-on-Write — it pretends to copy the data, but it doesn’t actually do it until you change something.
var moreNumbers = numbers // No real copy yet!
moreNumbers.append(4) // Now Swift makes a real copy here.
How It Connects to SwiftUI
Now here’s where it gets interesting.
SwiftUI views — like Text, VStack, or your own custom Views — are all structs.
That means every view in SwiftUI automatically benefits from this Copy-on-Write behavior.
When your state changes (for example, when a button is tapped), SwiftUI recreates your view hierarchy. It may sound heavy, but thanks to Copy-on-Write, it’s actually lightweight!
SwiftUI can reuse the same memory for unchanged parts of your UI, and only “copy” or rebuild what truly needs to be updated. So the next time your SwiftUI view refreshes after a @State change, remember: there’s a little bit of Copy-on-Write magic keeping everything smooth under the hood.