Notice: Undefined index: understanding-swift in /var/sites/hackingwithswift.com/site/src/Controller/NewBooksReadController.php on line 89
Why can variable properties in constant classes be changed? - a free tutorial

NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

Why can variable properties in constant classes be changed?

Paul Hudson    @twostraws   

One of the small but important differences between structs and classes is the way they handle mutability of properties:

  • Variable classes can have variable properties changed
  • Constant classes can have variable properties changed
  • Variable structs can have variable properties changed
  • Constant structs cannot have variable properties changed

The reason for this lies in the fundamental difference between a class and a struct: one points to some data in memory, whereas the other is one value such as the number 5.

Consider code such as this:

var number = 5
number = 6

We can’t simply define the number 5 to be 6, because that wouldn’t make sense – it would break everything we know about mathematics. Instead, that code removes the existing value assigned to number and gives it the number 6 instead.

That’s how structs work in Swift: when we change one of its properties, we are in fact changing the entire struct. Sure, behind the scenes Swift can do some optimization so that it isn’t really throwing away the whole value every time we change just one part of it, but that’s how it’s treated from our perspective.

So, if changing one part of a struct effectively means destroying and recreating the entire struct, hopefully you can see why constant structs don’t allow their variable properties to be changed – it would mean destroying and recreating something that is supposed to be constant, which isn’t possible.

Classes don’t work this way: you can change any part of their properties without having to destroy and recreate the value. As a result, constant classes can have their variable properties changed freely.

Hacking with Swift is sponsored by Instabug

SPONSORED Are you tired of wasting time debugging your Swift app? Instabug’s SDK is here to help you minimize debugging time by providing you with complete device details, network logs, and reproduction steps with every bug report. All data is attached automatically, and it only takes a line of code to setup. Start your free trial now and get 3 months off exclusively for the Hacking with Swift Community.

Start your free trial!

Sponsor Hacking with Swift and reach the world's largest Swift community!

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Link copied to your pasteboard.