Swift methods
Swift methods are functions associated with certain types
In Objective-C, classes are the only types that can define methods. But in Swift, you can not only choose whether to define a class/structure/enumeration, but also flexibly define methods on the types (classes/structures/enumerations) you create.
Instance Method
In the Swift language, an instance method is a method that belongs to an instance of a specific class, structure, or enumeration type.
Instance methods provide the following methods:
Can access and modify instance properties
Provide functions related to the purpose of the instance
Instance methods should be written between the surrounding braces ({}) of the type to which they belong.
Instance methods can implicitly access all other instance methods and properties of the type to which they belong.
Instance methods can only be called by a specific instance of the class to which they belong.
Instance methods cannot be called independently of the existing instance.
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 execution output of the above program is:
6 0
The Counter class defines three instance methods:
increment
Let the counter increment by one;##incrementBy(amount: Int)
Let the counter increment by a specified Increments an integer value;
#reset
resets the counter to 0.
CounterThis class also declares a mutable attribute
count, which is used to keep track of the current counter value.
Local parameter names and external parameter names of methodsSwift function parameters can have both a local name (used inside the function body) and an external name (used when calling the function Methods in Swift are very similar to methods in Objective-C. Like in Objective-C, the name of a method in Swift usually points to the first parameter of the method with a preposition, such as: with, for. , by, etc. Swift only gives a local parameter name to the first parameter name of the method; by default, it also gives the second and subsequent parameter names the following global parameter names. In the example, 'no1' is declared as a local parameter name in Swift. 'no2' is used for global declaration and access 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 is:
600 320 3666Yes. Provide external name settings We force to add an external name in the first parameter and use this local name as an external name (before Swift 2.0, the # sign was used)
On the contrary, we can also use underscores. (_) 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 result of the above program execution is:
2400 500 45000
self attribute#.
##Each instance of the type has an implicit attribute called self, which is completely equivalent to the instance itself.You can use this implicit self attribute in the instance method of an instance. 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 above program execution output is:
Self 内: 900 Self 内: 1500 结果为: 880 结果为: 850 结果为: 1480 结果为: 1450
Modify the value type in instance method
Structures and enumerations in the Swift language are value types. In general, properties of a value type cannot be modified within its instance methods.
However, if you really need to modify the properties of the structure or enumeration in a specific method, you can choose to mutate the method, 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.
The method can also assign a new instance to its implicit self attribute. This new instance will replace 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 execution output of the above program is:
9 15 270 450 81000 135000
Assign a value to self in the variable method
The variable method can assign a new implicit attribute self 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 execution output of the above program is:
39 65
Type method
Instance method is a method called by an instance of the type. You can also define the type The method called by itself is called a type method.
Declare the type methods of structures and enumerations, and add the keyword static before the func keyword of the method. Classes may use the keyword class to allow subclasses to override the implementation methods of the parent class.
Type methods are called with the dot (.) syntax like instance methods.
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 result of the execution of the above program is:
35 5