Original blog. Reprint please indicate the source
In the recent write code with Swift, despite some problems, but the amount of code has really lost a lot.
Swfit Novice Tutorial Series will continue to fix the update as I accumulate in the Swfit
Previous tutorials
Swift Single-instance mode specific explanation-thread safety, multi-core performance
Swift Novice Tutorial 4-collection (array,dictionary)
Swift Novice Tutorial 3-strings String
Swift Novice Tutorial 2-operator
Swift Novice Tutorial 1-Preparing knowledge
In Swift, the function has a Keywordfunc declaration
Format
Func function name (number of parameters 1, number of references 2,...) The return value {
}
Func firstfunction ()->int{var result = 4return result}println (firstfunction ())//Call
The number of parameters is null and the return value is int
In swift, functions can have random types of parameters. Because the SWIFT definition function itself is a type, the function itself can also return
function, or a function as a parameter of a function
first, the common use function way
1, no parameters, return value is empty
When there is no return value, you can omit
Func hwcfunction () {println ("Hello HWC")}
2, more than one number of parameters. return value Unique
Func hwcfunction (first:string,second:string)->string{return first+second}println (hwcFunction ("Hello", "HWC"))
3, multiple parameters, multiple return values
In Swift. Multiple return values often use tuples to return
Func hwcswapstring (first:string,second:string)-(string,string) {return (Second,first)}var (Second,first) = Hwcswapstring ("First", "second")
Second, external parameters
From the example above, we found that we did not see the information about the parameters when we used them. Is that we don't know what the first number of references is.
Meaning, what does the second parameter mean.
External parameters to solve the problem perfectly
Let's give a sample.
Func addpersonalinformation (name:string,sex:string)->string{return name+ ":" +sex}addpersonalinformation (" Wenchenhuang "," Male ")
For example, the input Wenchenhuang Male returns Wenchenhuang:male.
However, because there is no information, I do not know that the first parameter is the name of a representative. The second parameter represents gender. It is possible to use external
Number of references
Func addpersonalinformation (name Name:string,sex sex:string)->string{return name+ ":" +sex}addpersonalinformation (Name: "Wenchenhuang", Sex: "Male")
Call the time at a glance, and know which of the participants mean what
But it is still more troublesome to write like this. We have to provide internal and external two names for the same number of references
Swift provides us with a way to abbreviate external parameters
It is only necessary to prefix the internal parameters, the internal parameters will be reflected in the call
Func addpersonalinformation (#name: String, #sex: string)->string{return name+ ":" +sex}addpersonalinformation (name : "Wenchenhuang", Sex: "Male")
third, default parameters and variable parameters
The default is to have a default value for the number of parameters. At the time of the function call. Able to pass the value of this parameter. Also can not preach, do not preach the time to make
With default values
To give a simple example
Func hwcprint (Toprint:int = ten) {println (Toprint)}
Call the time
Hwcprint ()//Output 10hwcPrint (toprint:11)//Output 11
Here, it is not difficult to see that at the time of the call swift itself generated an external parameter name for us
This is a feature of Swift: The default parameter generates an external parameter name
A foreign currency name that can use an underscore as the default, so that Swfit does not provide an external parameter name. But this is not recommended.
Variable parameters
In swift, the argument passed in by the function is itself a constant. Cannot be changed in the function. Suppose you want this parameter to be a modified copy. The Add
Plus Var
Note: A function can have at most only one variable parameter and must be the last, in order to reduce the ambiguity of the content
Like what:
Func Hwcprint (toprint:int) {toprint = Toprint+1//wrongprintln (toprint)}func Hwcprint (var toprint:int) {ToPrint = Toprint+1//rightprintln (Toprint)}
iv. variable number of references
The so-called variable number of parameters. It's like a lot of scripting languages. The number of parameters is not understood, but inside the function body, the number of the parameters is stored in an array.
Variable parameters with ... Said
Give me a sample.
Func addmultistring (toprint:string ...) ->string{var resultstring:string = "" For tempstring in toprint{ Resultstring+=tempstring}return resultString} var result1 = addmultistring ("Hello", "World") var result2 = addmultistring ("Wen", "Chen", "Huang")
v. Input and OUTPUT parameters InOut
This type of argument is passed in to the original worthy reference. The operation of an incoming parameter within a function changes the original value, which is required when the reference is passed &
Func AddOne (inout input:int) {++input}var test1 = 10addOne (&test1)//test1 = 11func AddOne (var input:int) {++input}var Test2 = 10addOne (test3)//test2 = 10
vi. Types of functions
As mentioned earlier, in Swift, the function itself is a type, similar to a function pointer in the C language
BTW: The understanding of the function type is very important for the understanding of the following closures
For example: The above function func AddOne (input:int)
function type is (INT)---()
So. We can use function types like other types
var add: (Int,int)->int declares a entry argument is a function type variable of Int,int return value is Int add
Same. function type can also be passed as a parameter to a function
Func Addthree (addTwo: (int,int)->int,thrid:int)->int{return Addtwo+thrid}
function type can also be used as a return value
Here are the instructions in Apple's official documentation:
Func Stepforward (input:int), int {return input + 1} func Stepbackward (input:int), int {return input-1}func C Hoosestepfunction (Backwards:bool)-(int), int {return backwards?Stepbackward:stepforward}let Movenearertozero = choosestepfunction (CurrentValue > 0)
Seven, function nesting
What is function nesting, is to define functions in functions, the default nested functions are not visible to the outside. However, it can be called inside a function.
Overriding the above function
Func choosestepfunction (backwards:bool) (int), int {func stepforward (input:int), int {return input + 1} Func Stepbackward (input:int), Int {return input-1}return backwards? Stepbackward:stepforward}
Eight, Selector
In Swift, it is not recommended to use selector. Because it is not type-safe. But for the familiar Objective-c program ape classmate. Very many methods are not called. About it. Swift Selector direct incoming function name to Objective-c
Mybutton.addtarget (Self, Action: "buttontapped:", forControlEvents:. Touchupinside) Let timer = Nstimer (timeinterval:1, target:self, selector: "Test", Userinfo:nil, Repeats:false) func test {//do something}function buttontapped (sendr:anyobject) {//do something}
Swift Novice Tutorial Series 5-function +selector using in Swift