Updated for Xcode 14.2
In Swift, all copies of a class instance share the same data, meaning that any changes you make to one copy will automatically change the other copies. This happens because classes are reference types in Swift, which means all copies of a class all refer back to the same underlying pot of data.
To see this in action, try this simple class:
class User {
var username = "Anonymous"
}
That has just one property, but because it’s stored inside a class it will get shared across all copies of the class.
So, we could create an instance of that class:
var user1 = User()
We could then take a copy of user1
and change the username
value:
var user2 = user1
user2.username = "Taylor"
I hope you see where this is going! Now we’ve changed the copy’s username
property we can then print out the same properties from each different copy:
print(user1.username)
print(user2.username)
…and that’s going to print “Taylor” for both – even though we only changed one of the instances, the other also changed.
This might seem like a bug, but it’s actually a feature – and a really important feature too, because it’s what allows us to share common data across all parts of our app. As you’ll see, SwiftUI relies very heavily on classes for its data, specifically because they can be shared so easily.
In comparison, structs do not share their data amongst copies, meaning that if we change class User
to struct User
in our code we get a different result: it will print “Anonymous” then “Taylor”, because changing the copy didn’t also adjust the original.
If you want to create a unique copy of a class instance – sometimes called a deep copy – you need to handle creating a new instance and copy across all your data safely.
In our case that’s straightforward:
class User {
var username = "Anonymous"
func copy() -> User {
let user = User()
user.username = username
return user
}
}
Now we can safely call copy()
to get an object with the same starting data, but any future changes won’t impact the original.
SPONSORED From March 20th to 26th, you can join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.