struct Point { var x:Double var y:Double mutating func move(x:Double,y:Double) { self = Point(x: +x,y: +y) } static func name(){ print("Point") } } ()
1. Introduction
Methods are just a term, which actually combine functions with specific types. Classes, structures, and enumerations can define methods. Methods are divided into instance methods and type methods. Type methods are similar to class methods in Objective-C. One big difference between Swift and Objective-C is that Objective-C can only define methods in classes.
2. Basics of example methods
The syntax of the instance method is exactly the same as the function, and it is associated with the instance of the specific type. When the instance method is called, the instance point syntax of the type is called to complete some functional modules. Examples are as follows:
class Math { //Example method of completing addition function func add(param1:Double,param2:Double)->Double{ return param1+param2 } } //Create type instancevar obj = Math() //Calculate the method for calculation(5, param2: 5)
Similar to Objective-C, each class instance in Swift contains a self attribute hidden in it. The self attribute is the instance itself. Developers can use self in the instance method to call attributes or other instance methods. The examples are as follows:
class Math { //Example method of completing addition function func add(param1:Double,param2:Double)->Double{ return param1+param2 } func mul(param1:Double,param2:Double) -> Double { //Calling the instance method using self (param1, param2: param2) return param1*param2 } }
However, Swift does not require developers to write self. By default, developers can directly omit self to call properties and methods:
class Math { //Example method of completing addition function func add(param1:Double,param2:Double)->Double{ return param1+param2 } func mul(param1:Double,param2:Double) -> Double { //Calling the instance method using self add(param1, param2: param2) return param1*param2 } }
There is a situation that needs to be noted that for the call of attributes, if the parameter name in the method and the attribute name of the class instance are the same, self must be used to call the instance attribute of the class to prevent ambiguity:
class Math { var param1 = 10.0 //Example method of completing addition function func add(param1:Double,param2:Double)->Double{ // Here will use param1=10. If you do not add self, param1 in the parameter will be used. return self.param1+param2 } func mul(param1:Double,param2:Double) -> Double { //Calling the instance method using self add(param1, param2: param2) return param1*param2 } }
3. Modify the value of the value type in the instance method
First of all, we need to be clear about a concept. There are two types in Swift, value types and reference types. There are specific introductions in the sections of class, structure, and enumeration. It should be noted here that for value types, namely structures and enums, they cannot directly modify the value of the instance attribute in the instance method. Swift provides another way. If there is really such a requirement, developers can use the mutating keyword to declare the instance method as mutating. In fact, if the value of the value type attribute is modified in the mutating instance method, a new instance will be created instead of the original instance. The example is as follows:
struct Point { var x:Double var y:Double mutating func move(x:Double,y:Double) { +=x +=y } } var point = Point(x: 1, y: 1) print(point) (3, y: 3) print(point)
Modifying the value of a property in a variable method of a value type instance is actually creating a new instance. The above writing method is the same as the following writing principle:
struct Point { var x:Double var y:Double mutating func move(x:Double,y:Double) { self = Point(x: +x,y: +y) } }
IV. Type method
Just as an instance method is called through an instance of a type, the type method is called directly through a type. Compared with an instance method, self in a type method refers to the current type, and developers can also use self to distinguish between type attributes and parameters in a type method. Use the Static keyword to create type methods:
If you create a type method in a class, if this method can be rewritten by a subclass, you should use the class keyword to create it. The example is as follows:
class Math { var param1 = 10.0 //Example method of completing addition function func add(param1:Double,param2:Double)->Double{ // Here will use param1=10. If you do not add self, param1 in the parameter will be used. return self.param1+param2 } func mul(param1:Double,param2:Double) -> Double { //Calling the instance method using self add(param1, param2: param2) return param1*param2 } class func name(){ print("Math") } } ()