A simple explanation
As with other object-oriented languages, the methods in swift can be divided into 2 categories:
(1) Example method (Instance Methods)
In OC, the instance method starts with a minus sign (-)
(2) Type method (Type Methods)
In OC, the type method starts with a plus sign (+)
Ii. Examples and methods
1. What is an instance method?
Instance method: Is a method that can only be called with an object instance, or it can be called an object method
The syntax of an instance method is basically the same as a function
2. code example:
1 class Dog {2 func run () {3 println ("dog--->run" )4 }5}6 var d = Dog () 7 D.run ()
Description
Line 2nd defines a run method
Line 7th calls the Run method: The calling method is much like the calling function, and the format is "object name. Method Name (parameter)"
Iii. parameters of the method
(1) The parameters of the method and function are somewhat different, by default
The 1th parameter name of a method is just a local parameter name
The other parameter names of the method (except for the 1th parameter name) are both local and external parameter names
1 class Calculator {2 func sum (num1:int, num2:int), Int {3 return NUM1 + num24 }5}6 var c = Calculator () 7 c.sum (ten)
Description
NUM1 is only a local parameter name, NUM2 is both a local parameter name and an external parameter name
Equivalent to Func sum (num1:int, #num2: int), int
(2) You can add an underscore in front of the parameter name to remove the default external parameter name
1 class Calculator {2 func sum (num1:int, _ Num2:int), Int {3 return NUM1 + num24 }5}6 var c = Calculator () 7 c.sum (ten)
Description: NUM1, num2 are only local parameter names, not external parameter names
(3) You can also add an external parameter name to the 1th parameter
1 class Calculator {2 func sum (#num1: int, num2:int), int {3 return NUM1 + num24 }5}6 var c = Calculator () 7)
Description: NUM1, num2 is both a local parameter name and an external parameter name
Iv. Types of methods
1. What is a type method?
Methods that are modified by the keyword class, also known as "class methods"
1 class Calculator {2 class func sum (Num1:int, num2:int), Int {3 return NUM1 + num24}5}6 calculator.s Um (ten, num2:20)
Description
Line 2nd defines a type method
The characteristics of a type method: Invoking a type method directly with a class, cannot invoke a type method with an object
Line 6th invokes the type method, in the form "class name. Method Name (parameter)"
Note: The method name of the type method and the instance method can be the same
1 classCalculator {2 classFunc sum (num1:int, Num2:int)Int {3 returnNUM1 +num24 }5Func sum (num1:int, Num2:int)Int {6 returnNUM1 +num27 }8 }9Calculator.sum (Ten, num2: -)Tenvar C =Calculator () OneC.sum (Ten, num2: -)
Description
Line 2nd defines the type method and the instance method defined by line 5th: The same as the method name
Line 9th uses the class invocation type method
Line 11th using an object to invoke an instance method
V. Self
1. Brief description
Within each method, there is an implicit property of self, which is essentially consistent with the use of self in objective-c.
What does self represent? Who calls this method, self represents who
(1) In an instance method: Self represents an object that invokes a method
(2) In a type method: Self represents a class that invokes a method
2. Code examples
1 classPerson {2 classfunc Run () {3println"class Func Run")4 }5 func Run () {6println"Func Run")7 }8 classfunc Test () {9 Self.run ();Ten } One func Test () { A Self.run (); - } - } thevar p =Person () - p.test () -Person.test ()
The output of the program is:
Func Run
class Func Run
iOS Development Swift Chapter-(10) method