classCounter{varcount=0funcincrement(){count++}funcincrementBy(amount:Int){count+=amount}funcreset(){count=0}}letcounter=Counter()// the initial counter value is 0counter.increment()// the counter's value is now 1counter.incrementBy(5)// the counter's value is now 6counter.reset()// the counter's value is now 0
Local and External Parameter Names for Methods
123456789101112131415161718192021222324
// Specifically, Swift gives the first parameter name in a method a local parameter name by default, and gives the second and subsequent parameter names both local and external parameter names by default.classCounter{varcount:Int=0funcincrementBy(amount:Int,numberOfTimes:Int){count+=amount*numberOfTimes}}letcounter=Counter()counter.incrementBy(5,numberOfTimes:3)// counter value is now 15// It is behaved as the same as putting a '#' symbol before the namefuncincrementBy(amount:Int,#numberOfTimes:Int){count+=amount*numberOfTimes}// This will not allow external namefuncincrementBy(amount:Int,_:Int){count+=amount*numberOfTimes}
The self Property
123456789101112131415
funcincrement(){self.count++}structPoint{varx=0.0,y=0.0funcisToTheRightOfX(x:Double)->Bool{returnself.x>x}}letsomePoint=Point(x:4.0,y:5.0)ifsomePoint.isToTheRightOfX(1.0){println("This point is to the right of the line where x == 1.0")}// prints "This point is to the right of the line where x == 1.0"
Modifying Value Types from Within Instance Methods
1234567891011121314151617
structPoint{varx=0.0,y=0.0mutatingfuncmoveByX(deltaX:Double,ydeltaY:Double){x+=deltaXy+=deltaY}}varsomePoint=Point(x:1.0,y:1.0)somePoint.moveByX(2.0,y:3.0)println("The point is now at (\(somePoint.x), \(somePoint.y))")// prints "The point is now at (3.0, 4.0)"// constant instance's properties cannot be modifiedletfixedPoint=Point(x:3.0,y:3.0)fixedPoint.moveByX(2.0,y:3.0)// this will report an error
Assigning to self Within a Mutating Method
12345678910111213141516171819202122232425
structPoint{varx=0.0,y=0.0mutatingfuncmoveByX(deltaX:Double,ydeltaY:Double){self=Point(x:x+deltaX,y:y+deltaY)}}enumTriStateSwitch{caseOff,Low,Highmutatingfuncnext(){switchself{caseOff:self=LowcaseLow:self=HighcaseHigh:self=Off}}}varovenLight=TriStateSwitch.LowovenLight.next()// ovenLight is now equal to .HighovenLight.next()// ovenLight is now equal to .Off
classSomeClass{classfuncsomeTypeMethod(){// type method implementation goes here}}SomeClass.someTypeMethod()// Within the body of a type method, the implicit self property refers to the type itself, rather than an instance of that type. For structures and enumerations, this means that you can use self to disambiguate between static properties and static method parameters, just as you do for instance properties and instance method parameters.structLevelTracker{staticvarhighestUnlockedLevel=1staticfuncunlockLevel(level:Int){iflevel>highestUnlockedLevel{highestUnlockedLevel=level}}staticfunclevelIsUnlocked(level:Int)->Bool{returnlevel<=highestUnlockedLevel}varcurrentLevel=1mutatingfuncadvanceToLevel(level:Int)->Bool{ifLevelTracker.levelIsUnlocked(level){currentLevel=levelreturntrue}else{returnfalse}}}classPlayer{vartracker=LevelTracker()letplayerName:StringfunccompletedLevel(level:Int){LevelTracker.unlockLevel(level+1)tracker.advanceToLevel(level+1)}init(name:String){playerName=name}}varplayer=Player(name:"Argyrios")player.completedLevel(1)println("highest unlocked level is now \(LevelTracker.highestUnlockedLevel)")// prints "highest unlocked level is now 2"player=Player(name:"Beto")ifplayer.tracker.advanceToLevel(6){println("player is now on level 6")}else{println("level 6 has not yet been unlocked")}// prints "level 6 has not yet been unlocked"