Source
Methods are divided into two types:
- Instance method (Instance Methods)
- Type method (Type Methods)
method is basically the same as a function
instance method (Instance Methods)
Define an instance method:
class Counter { var count = 0 func increment () { count+ + } Func IncrementBy (amount:int) { + = amount } func reset () {= 0
Call Method:
Let counter =////Counter.incrementby (5// //
parameter name of the method
- The first parameter name of the Swift default method is used only as an internal parameter name. If you need swift to extend it to an external parameter name, you need to add "#" to the parameter name to implement
- The parameter names of the second and subsequent parameters of the method are automatically extended to both internal and external parameter names. To block this automatic extension, you can add "-" to the name of the parameter to implement
class Counter { var count:int = 0 func incre (Amount:int, number:int) { + = Amount * number
The Incre method has two parameters: Amount and number. By default, Swift only treats amount as a local name, but treats number as a local name and as an external name. This method is called below:
Let counter = counter () Counter.incrementby (5, numberoftimes:3//
The Self attribute
Self represents the instance itself
The increment method in the above example can also be written like this:
func increment () { Self.count+ +
Change (mutating) method
In general, a property of a value type cannot be modified in its instance method, but can be modified using the change method.
Defining a mutation method requires placing the mutating keyword before func
Define the Change method
struct Point { var x = 0.0, y = 0.0 mutating func movebyx (deltax:double, y deltay:do uble) { + = deltax+ = DeltaY
Using the Change method
var somepoint = Point (x:1.0, y:1.0) Somepoint.movebyx (2.0, y:3.0) println at (\ (somepoint.x), \ (SOMEPOINT.Y)) "//
assigning self to a change method
struct Point { var x = 0.0, y = 0.0 mutating func movebyx (deltax:double, y deltay:double ) { = point (x:x + deltax, y:y + deltay)
Methods for changing enumerations
enum Tristateswitch { CaseOff, Low, high mutating func next () {SwitchSelf { Caseoff:self= Low Caselow:self= High Casehigh:self=Off} }}varOvenlight =tristateswitch.low ovenlight.next ()//ovenlight now equals. HighOvenlight.next ()//ovenlight now equals. Off
An enumeration of three-state switches is defined in the example above. Each time the next method is called, the switch loops before a different power state (Off,low,high).
type method (Type Methods)
- Declare the type method of the class, and precede the Func keyword of the method with the keyword class
- Declares the struct and enum type methods, plus the keyword static before the method's func keyword.
Declaring a class's type method
class SomeClass { class func Sometypemethod () { //
declaring the type method of a struct body
struct Leveltracker { var highestunlockedlevel = 1
static func Unlocklevel (level:int) { If level > Highestunlockedlevel {
Highestunlockedlevel = level
} } static func levelisunlocked (Level:int), Bool { return level <= Highestunlockedlevel
2015-03-21
20:27:29
13th Chapter: Methods