Skip to content

Instantly share code, notes, and snippets.

@jemygraw
Created June 14, 2015 07:54
Show Gist options
  • Select an option

  • Save jemygraw/10e9673f8729127090c7 to your computer and use it in GitHub Desktop.

Select an option

Save jemygraw/10e9673f8729127090c7 to your computer and use it in GitHub Desktop.
learn about willSet and didSet, explore the class methods parameters usage
//willSet and didSet
class Shape{
var oldName :String
var newName :String
var name :String{
willSet{
self.oldName=name
self.newName=newValue
println("WillSet: old values is \(name), new values is \(newValue)")
}
didSet{
println("DidSet: old value is \(oldValue), new values is \(name))")
}
}
init(name:String){
self.name=name
self.newName=""
self.oldName=""
}
func desc(){
println("my name is \(self.name)")
}
}
var s1=Shape(name:"jemy")
s1.desc()
s1.name="orange"
s1.desc()
//class methods
class Counter{
var count :Int = 0
func incBy(amount :Int, numberOfTimes times:Int){
self.count += amount * times
}
func incBy2(amount :Int, times:Int)
{
self.count += amount * times
}
func incBy3(amount :Int, times :Int?)
{
var innerTimes=1
if times != nil{
innerTimes=Int((times?.value)!)
}
self.count += amount * innerTimes
}
}
var counter = Counter()
counter.incBy(2, numberOfTimes: 7)
println(counter.count)
counter.incBy(2, numberOfTimes: 10)
println(counter.count)
counter.incBy2(10, times: 10)
println(counter.count)
counter.incBy3(100, times: nil)
println(counter.count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment