Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

grandM

macrumors 68000
Original poster
Oct 14, 2013
1,568
329
Hi guys!

I have two pieces of code. Both are working. They are using structures.
Code:
struct CalculatorBrain {

    var result: Double = 0 {

        didSet(oldResult){

            print("the result changes from \(oldResult) to \(result)")

        }

    }

    mutating func add(_ augend: Double){

        result +=  augend

    }

}


var myCalculatorBrain = CalculatorBrain()

myCalculatorBrain.result

myCalculatorBrain.add(15.2)

In this code I needn't initialize result. Probably because it was already done in the definition. What troubles me though is the code beneath. It also works until I change the let of surName to a var. Suddenly the memberwise initializer expects me to initialize surName. This seems contradictory to the code above?

Code:
struct Name {

    let surName: String = "Doo"

    var firstName: String

 

    var fullName: String {

        return firstName + " " + surName

    }

}

var nameFirstKid = Name(firstName: "John")

nameFirstKid.fullName

var nameSecondKid = Name(firstName: "Josephine")

nameSecondKid.fullName

nameSecondKid.firstName = "Claire"

nameSecondKid.fullName

Is think it's because the initializer of Name still requires a parameter and CalculatorBrain doesn't?
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.