Swift
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Optionals
-
Section 4: Object-Oriented and Value Types
-
Section 5: Memory Management
-
Section 6: Generics and Error Handling
-
Section 7: Concurrency
-
Section 8: Working with Collections
-
Section 9: Codable and Data Handling
-
Section 10: Protocol-Oriented Programming
-
Section 11: Testing and Tooling
-
Section 12: Practical Projects
-
Section 13: Interview Practice
-
Section 14: More Practice Exercises
-
Section 15: More Standard Library
-
Section 16: Advanced Concurrency
-
Section 17: Foundation Framework Deep Dive
-
Section 18: URLSession and Networking Deep Dive
-
Section 19: Combine Framework
-
Section 20: SwiftUI Fundamentals for Swift Developers
-
Section 21: Server-Side Swift with Vapor
-
Section 22: Swift Package Manager Deep Dive
-
Section 23: Swift Concurrency Deep Dive
-
Section 24: More Language Features
-
Section 25: Error Handling Deep Dive
-
Section 26: Testing Deep Dive
-
Section 27: Data Structures and Algorithms in Swift
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Swift Macros (Swift 5.9+)
-
Section 31: Property Wrappers Ecosystem
-
Section 32: Swift Interop Deep Dive
-
Section 33: iOS App Architecture Patterns
-
Section 34: Performance and Debugging
-
Section 35: App Distribution and CI/CD
-
Section 36: More Practical Projects
-
Section 37: SwiftData and Persistence
-
Section 38: More Design Patterns
-
Section 39: More Review and Practice
-
Section 40: More Foundation Deep Dive
-
Section 41: Advanced Collections in Swift
-
Section 42: Advanced Generics Practice
-
Section 43: UIKit for Legacy and Hybrid Apps
-
Section 44: watchOS and visionOS Development Basics
-
Section 45: More Networking Patterns
-
Section 46: More Testing Practice
-
Section 47: Accessibility in Swift Apps
-
Section 48: Localization
-
Section 49: More Practical Projects Round 2
-
Section 50: Swift Charts Framework
-
Section 51: More Interview and Algorithm Practice
-
Section 52: Final Practice and Mastery
-
Section 53: Swift Compiler and Build System
-
Section 54: More Concurrency Practice
-
Section 55: App Store Guidelines and Review
-
Section 56: More Design and Architecture
25: Value Semantics and Copy-on-Write
I've noticed a recurring pattern when I mentor developers moving from C++ or Java to Swift. They get a handle on the difference between struct and class, but then they hit a wall of performance anxiety. They start worrying that passing a massive array around their app is going to kill the frame rate because, as they've been told, "structs are value types, so they're copied every time."
The "Structs are Slow for Big Data" Myth
The misconception is that if you have an array with 100,000 elements and you assign it to a new variable, Swift immediately duplicates those 100,000 elements in memory. If that were true, Swift would be unusable for high-performance apps. Let's look at what actually happens.
struct UserProfile {
var username: String
var bio: String
}
var groupA = [UserProfile](repeating: UserProfile(username: "Dev", bio: "Coding..."), count: 100_000)
var groupB = groupA // Is this a massive, slow copy?
In the code above, the assignment var groupB = groupA is nearly instantaneous. It doesn't matter if the array has ten elements or ten million. Why? Because Swift is lying to you. Or rather, it's being very clever.
How Copy-on-Write Actually Works
Swift uses a technique called Copy-on-Write (CoW). When you assign one collection to another, Swift doesn't copy the actual data. Instead, both variables point to the same memory storage. They share the same underlying buffer.
The "copy" only happens at the exact moment you try to mutate one of them. Here is the mental model: Swift keeps a reference count on the internal storage. As long as only one variable owns that storage, it can be modified in place. The moment a second variable claims ownership, Swift marks the storage as shared. If you then try to change a value in groupB, Swift notices the shared ownership, copies the data to a new buffer, and then applies the change to that new copy.
groupB[0].username = "NewName" // NOW the copy happens.
This gives you the best of both worlds: the safety and predictability of value semantics (meaning groupA won't magically change just because you edited groupB) with the performance of reference types.
Where CoW Stops and Value Semantics Continue
Here is the part where people usually get tripped up again. CoW is not a magic property of all structs. It is an optimization implemented specifically by the Swift Standard Library for collections like Array, Dictionary, Set, and String.
If you build your own custom struct that doesn't wrap one of these collections, you don't get CoW for free. Consider this:
struct BigData {
var values: (Int, Int, Int, Int, Int) // A large tuple
}
var data1 = BigData(values: (1, 2, 3, 4, 5))
var data2 = data1 // This IS a real, immediate copy.
Because BigData is a simple struct containing a tuple (not a managed collection), Swift performs a bit-for-bit copy of the data on assignment. For small structs, this is incredibly fast—often faster than the overhead of CoW. But if you're building a custom type that manages a massive amount of memory, you'll have to implement your own CoW logic using a private reference-type wrapper. I'll leave that for the advanced architecture discussions, but for now, just remember: value semantics are the guarantee (the data won't change unexpectedly), while CoW is the optimization (we won't copy until we have to).
📋 Practical Task
Implementing a Copy-on-Write Storage Wrapper
To truly understand how the Swift Standard Library implements CoW, you're going to build a simplified version of it. You will create a CustomBuffer struct that manages a large piece of data without copying it until a mutation occurs.
Requirements:
- Create a private class called
BufferStoragethat holds an array of integers. This class will act as the shared reference. - Create a struct called
CustomBufferthat holds a reference to an instance ofBufferStorage. - Implement a method
updateValue(at index: Int, to value: Int)insideCustomBuffer. - Inside
updateValue, useisKnownUniquelyReferencedto check if the storage is shared. If it is, create a new copy of theBufferStoragebefore modifying the value. - Verify your implementation by creating two
CustomBufferinstances, assigning one to the other, and ensuring that updating one does not affect the other.
There are no comments for now.