WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Day 10: Structs Part 1 - MUTATING FUNC versus VAR

Forums > 100 Days of SwiftUI

I can't discern the difference between using a mutating function, which Paul makes use of here, and dynamic property values, which he applies here. They seem to do the same thing in a simple setting, so the difference must be for more complicated code.

For example, I can get two identical results from the following (borrowing from some of Paul's code on the 10.2 review/test):

struct Candle {
    var burnLength: Int
    var alreadyBurned = 4
    mutating func burnRemaining() -> Int {
        return burnLength - alreadyBurned
    }
    var burnRemainingVar: Int {
        return burnLength - alreadyBurned
    }
}
var myCandle = Candle(burnLength: 5, alreadyBurned: 2)
myCandle.burnRemaining()
myCandle.burnRemainingVar

Both .burnRemaining() and .burnRemainingVar produce the same value (3, obviously). Aside from the function code requiring a few more typed characters, why would I want to use one 'method' (pun not intended) rather than the other?

1      

Welcome to the Forums.

Here we either (1) Tell You the Answer or (2) Teach You More Than You Want To Know.

let typeOfAnswer = Int.random(in: 1...2)
if typeOfAnswer.isMultiple(of: 2) {
    print("Today you get lots of detail.")
} else {
    print("Lucky you. Today just the answer.")
}

Today you get lots of detail

Mutating mean changing.

In your examples, you aren't changing, you're just using. So sure, both your examples work just fine. Paste this code into Playgrounds and remove the mutating key word. How does the compiler respond?

// Paste into Playgrounds.
struct Candle {
    var burnLength: Int
    var alreadyBurned = 4

    var burnRemainingVar: Int {
        // This is NOT changing anything.
        burnLength - alreadyBurned  // by the way, return isn't necessary here.
    }
    func burnRemaining() -> Int {
        // Also, NOT changing anything.
        return burnRemainingVar // just pass the var back.
    }
    // Try this without the mutating keyword. Compiler will complain.
     mutating func burnRemaining(thisLong: Int) -> Int {
        alreadyBurned += thisLong // MUTATE the var
        return burnRemaining()  // return IS required here. Do you know why?
    }
}

// Test cases
var myCandle = Candle(burnLength: 5, alreadyBurned: 2)
myCandle.burnRemainingVar  // print the var value
myCandle.burnRemaining()   // execute a NON mutating function
myCandle.burnRemaining(thisLong: 2) // execute a MUTATING function

Keep asking questions!

1      

Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Fernando's book will guide you in fixing bugs in three real, open-source, downloadable apps from the App Store. Learn applied programming fundamentals by refactoring real code from published apps. Hacking with Swift readers get a $10 discount!

Read the book

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.