importCocoa// ## privatestructBankAccount{privatevarfunds=0}/*
* Use private for “don’t let anything outside the struct use this.”
* Use private(set) for “let anyone read this property, but only let my methods write it.”
* Use fileprivate for “don’t let anything outside the current file use this.”
* Use public for “let anyone, anywhere use this.”
*/// ## static variable and functionstructSchool{staticvarstudentCount=0staticfuncadd(student:String){print("\(student) joined the school.")studentCount+=1}}// ## self and Self/*
To access static code from non-static code,
always use your type’s name such as School.studentCount.
You can also use Self to refer to the current type.
*/// ## singletonstructEmployee{letusername:Stringletpassword:Stringstaticletexample=Employee(username:"abc",password:"xyz")}// ## Check point/*
create a struct to store information about a car,
including its model, number of seats, and current gear,
then add a method to change gears up or down.
Have a think about variables and access control:
what data should be a variable rather than a constant,
and what data should be exposed publicly?
Should the gear-changing method validate its input
*/structCar{letmodel:StringletnumberOfSeats:IntprivatevarcurrentGear:Intinit(model:String,numberOfSeats:Int){self.model=modelself.numberOfSeats=numberOfSeatsself.currentGear=0}mutatingfuncgearUp()->Bool{ifself.currentGear==6{returnfalse}self.currentGear+=1returntrue}mutatingfuncgearDown()->Bool{ifself.currentGear==0{returnfalse}self.currentGear-=1returntrue}}
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)