Swift method
The Swift method is a function associated with a specific type
In Objective-C, a class is the only type that defines a method. But in Swift, you can not only choose whether you want to define a class/structure/enumerate, but also have the flexibility to define methods on the type you create (class/structure/enumerate).
The instance method
In the Swift language, the instance method is a method that belongs to a particular class, structure, or enumerates an instance of a type.
The instance method provides the following methods:
-
Instance properties can be accessed and modified
-
Provides functionality related to the purpose of the instance
The instance method is written between the front and back braces of the type to which it belongs.
An instance method has implicit access to all other instance methods and properties of the type to which it belongs.
An instance method can only be called by a specific instance of the class to which it belongs.
Instance methods cannot be called separately from existing instances.
Grammar
func funcname(Parameters) -> returntype { Statement1 Statement2 …… Statement N return parameters }
Instance
import Cocoa class Counter { var count = 0 func increment() { count++ } func incrementBy(amount: Int) { count += amount } func reset() { count = 0 } } // 初始计数值是0 let counter = Counter() // 计数值现在是1 counter.increment() // 计数值现在是6 counter.incrementBy(5) print(counter.count) // 计数值现在是0 counter.reset() print(counter.count)
The output of the above program execution is:
6 0
The Counter class defines three instance methods:
-
increment
the counter press an increment; -
incrementBy(amount: Int)
counter increment by a specified integer value; -
reset
resets the counter to 0.
Counter
class also declares a variable property,
count
is used to keep track of the current counter value.
The local and external parameter names of the method
Swift function parameters can have both a local name (used inside the function body) and an external name (used when the function is called).
The methods in Swift are very similar to 2 in Objective-C. As in Objective-C, the name of a method in Swift usually points to the first argument of the method in a preposition, such as: with, for, by, and so on.
Swift defaults to only one local parameter name for the first argument name of the method; The default is to give the second and subsequent parameter names the global parameter names.
'no1' is declared as a local parameter name in swift in the following instance. 'no2' is used for global claims and is accessed through external programs.
import Cocoa class division { var count: Int = 0 func incrementBy(no1: Int, no2: Int) { count = no1 / no2 print(count) } } let counter = division() counter.incrementBy(1800, no2: 3) counter.incrementBy(1600, no2: 5) counter.incrementBy(11000, no2: 3)
The output of the above program execution is:
600 320 3666
Whether to provide an external name setting
We force the local name to be used as an external name when we add an external name to the first argument (Swift 2.0 is used before the hashtag).
Instead, we can also use underscores to set the second and subsequent parameters without providing an external name.
import Cocoa class multiplication { var count: Int = 0 func incrementBy(first no1: Int, no2: Int) { count = no1 * no2 print(count) } } let counter = multiplication() counter.incrementBy(first: 800, no2: 3) counter.incrementBy(first: 100, no2: 5) counter.incrementBy(first: 15000, no2: 3)
The output of the above program execution is:
2400 500 45000
Self property
Each instance of a type has an implicit property called self, which is exactly the same as the instance itself.
You can use this implicit self property in an instance's instance method to refer to the current instance.
import Cocoa class calculations { let a: Int let b: Int let res: Int init(a: Int, b: Int) { self.a = a self.b = b res = a + b print("Self 内: \(res)") } func tot(c: Int) -> Int { return res - c } func result() { print("结果为: \(tot(20))") print("结果为: \(tot(50))") } } let pri = calculations(a: 600, b: 300) let sum = calculations(a: 1200, b: 300) pri.result() sum.result()
The output of the above program execution is:
Self 内: 900 Self 内: 1500 结果为: 880 结果为: 850 结果为: 1480 结果为: 1450
Modify the value type in the instance method
Structures and enumerations are value types in the Swift language. In general, a property of a value type cannot be modified in its instance method.
However, if you do need to modify a structure or enumerated properties in a specific method, you can select the method mutating, and then the method can change its properties from within the method, and any changes it makes will remain in the original structure at the end of the method.
Method can also assign a completely new instance to its implied self property, which replaces the original instance after the method ends.
import Cocoa struct area { var length = 1 var breadth = 1 func area() -> Int { return length * breadth } mutating func scaleBy(res: Int) { length *= res breadth *= res print(length) print(breadth) } } var val = area(length: 3, breadth: 5) val.scaleBy(3) val.scaleBy(30) val.scaleBy(300)
The output of the above program execution is:
9 15 270 450 81000 135000
Assign a value to self in a variable method
Variable methods can give the implied property self a whole new instance.
import Cocoa struct area { var length = 1 var breadth = 1 func area() -> Int { return length * breadth } mutating func scaleBy(res: Int) { self.length *= res self.breadth *= res print(length) print(breadth) } } var val = area(length: 3, breadth: 5) val.scaleBy(13)
The output of the above program execution is:
39 65
Type method
An instance method is a method called by an instance of a type, and you can also define the method called by the type itself, which is called a type method.
Declares the structure and enumerates the type method, adding the keyword static before the method's func keyword. The class may use the keyword class to allow the child class to override the implementation method of the parent class.
The type method uses the same dot number (.) as the instance method The syntax call.
import Cocoa class Math { class func abs(number: Int) -> Int { if number < 0 { return (-number) } else { return number } } } struct absno { static func abs(number: Int) -> Int { if number < 0 { return (-number) } else { return number } } } let no = Math.abs(-35) let num = absno.abs(-5) print(no) print(num)
The output of the above program execution is:
35 5