I know that Swift assumes that you are referencing a property or method of the current instance whenever you use a known property or method name; however, I want to make sure I understand the use of self
given that it's in heavy use in code prior to Swift.
class School {
var numberOfBooks = 0
var numberofPens = 0
func buyBooks() {
self.numberOfBooks++
}
func buyPens() {
self.numberOfPens++
}
func readyForSchool() {
if self.numberOfBooks && self.numberOfPens >= 1 {
println("I am ready for school")
} else {
println("I need to buy school materials")
}
}
}
var preparedForSchool = School()
preparedForSchool.buyBooks()
preparedForSchool.buyPens()
preparedForSchool.readyForSchool() \\returns "I am ready for school"