The writing format and usage of functions and methods, enumerations, etc. are very different from those of OC or.
First, the function
1.1 No return value, invisible parameter
Func MyTest2 () { println ("Hello")}
1.2 has a return value, a tangible parameter
Func myTest1 (Num1:int, Num2:int), string{ return String (NUM1 + num2)}
1.3 external parameters . ( equivalent to an alias for the parameter , to facilitate the identification of memory )
Func myTest3 (myNum1 num1:int, myNum2 num2:int) { println (num1 + num2)}mytest3 (mynum1:1, mynum2:2)//Call Func MyTest4 (#num1: int, #num2: int) { //preceded by # indicates direct parameter name as external parameter name println (num1 + num2)}mytest4 (Num1:2, Num2:3)
1.4 Default Parameters . ( similar to C + +)
Func myTest5 (num1:int, num2:int = ten) { println (num1+num2)}mytest5 (1) myTest5 (1, num2:22) // The following parameter defaults to Func MyTest6 (Num1:int, _ Num2:int = ten) with formal parameters as the external parameter name , and the external parameter name of the parameter is ignored, with the println (num1+num2)}mytest6 before the argument ( 1, 22)
1.5 Constants and variable arguments . ( by default , Swift 's parameters are constant parameters and cannot be modified. )
Other languages, such as C, do not have this concept. You can modify the Func myTest7 (let Num1:int, Num2:int) {// num2 = ten //This sentence is wrong and cannot be modified println (num1+num2)}func MyTest8 ( var num1:int, Num2:int) { ///plus var can be changed to variable arguments. num1 = println (num1+num2)}
Second, assert
and C basically the same, nothing to say
var a = 10assert (a>5, "right")
third, enumeration
Not as in C , the default value is assigned to 0,1...
3.1 Enumeration format
Enum compasspoint{case North //case indicates that a new row member is defined case South case East case West case A, B, C //Multiple members can also appear on the same line}
3.2 Access Enumeration
var direct = Compasspoint.northdirect =. South //Because the previous access, after the automatic inference. Do not write enumeration name//COMPASSPOINT.A
3.3 enumeration combined with switch use
Switch Direct{case. North: println ("Lots of Planets has a") case. South: println (' Watch out for Penguins ') case. East: println ("Where The Sun Rises") case. West: println ("Where The Skies is Blue") Default: println ("OK")}
3.4 Associative enumeration . Values that can be associated with changes
Enum barcode{case UPCA (int,int,int) //associated with 3 parameters case QRCode (String) //associated with a string}//var Productbarcode = BARCODE.UPCA (0, 1, 2) //This is used, associating 3 numbers.
3.5 enumerates the original values .
Enum myascii:character{ //definition type case Tab = "\ t" case linefeed = "\ n"}enum planet:int {case Mercury = 1, Venus, Earth, Mars, Jupite, Saturn, Uranus, neptune}//use the Toraw method to access the original value of the enumeration println (Planet.Venus.toRaw ()) //value 2, This auto-increment//uses Fromraw to find the original enumeration member println (Planet.fromraw (2)) //Returns an optional type, which means that there is not necessarily
Evaluation: Adds new functionality to older things, simplifying the notation of functions in OC. For the use of OC or slightly do not adapt.
Reference:
The Swift programming Language
Apple Dev Center
Reprint Please specify source: http://blog.csdn.net/xn4545945
Functions, assertions, enumerations, and so on in "IOS" Swift