importCocoa// ## Define structstructEmployee{letname:StringvarvacationRemaining:IntmutatingfunctakeVacation(days:Int){ifvacationRemaining>days{vacationRemaining-=daysprint("I'm going on vacation!")print("Days remaining: \(vacationRemaining)")}else{print("Oops! There aren't enough days remaining.")}}}// ## Computed Properties with getter and setterstructEmployee2{letname:StringvarvacationAllocated=14varvacationTaken=0varvacationRemaining:Int{// getterget{vacationAllocated-vacationTaken}// setter -> even we have a setter here, no `mutating` needs to be addedset{vacationAllocated=vacationTaken+newValue}}}// ## Property observers// willSet and didSetstructApp{varcontacts=[String](){// newValue and oldValue are built-in variable in observerswillSet{print("Current value is: \(contacts)")print("New value will be: \(newValue)")}didSet{print("There are now \(contacts.count) contacts.")print("Old value was \(oldValue)")}}}// ## custom initializers// explicitly define `init` function - no `func` needed, and you can use `self.`structPlayer{letname:Stringletnumber:Intinit(name:String){self.name=namenumber=Int.random(in:1...99)}}
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)