What if you could update one thing and see the change everywhere instantly?
Why classes exist alongside structs in Swift - The Real Reasons
Imagine you want to organize information about many people, like their names and ages, and also keep track of changes to their details in different parts of your app.
If you try to copy all the details every time you want to update or share them, it becomes slow and confusing. You might accidentally change one copy but forget to update others, causing mistakes.
Classes let you create one shared object that many parts of your app can use and change together. This way, updates happen in one place, keeping everything in sync easily.
var person1 = Person(name: "Anna", age: 30) var person2 = person1 person2.age = 31 // person1.age is still 30
class Person { var name: String var age: Int init(name: String, age: Int) { self.name = name self.age = age } } var person1 = Person(name: "Anna", age: 30) var person2 = person1 person2.age = 31 // person1.age is now 31
It enables shared, changeable data that stays consistent everywhere it's used.
Think of a music app where the same song object is used in playlists and the player; changing the song's info updates it everywhere instantly.
Structs copy data, so changes don't affect the original.
Classes share a single object, so changes update everywhere.
Classes are great when you want shared, changeable data.