why is the memberwise initializer different?

Discussion in 'iOS Programming' started by grandM, Jan 11, 2017.

  1. grandM, Jan 11, 2017
    Last edited: Jan 11, 2017

    grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #1
    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?
     

Share This Page