[IOS] functions, assertions, enumeration, and iosswift in Swift

Source: Internet
Author: User

[IOS] functions, assertions, enumeration, and iosswift in Swift

The writing formats and usage of functions, methods, and enumerations vary greatly compared with OC.

I. Functions

1.1 no return value, invisible Parameter

func myTest2(){    println("Hello")}

1.2 return value and Parameter

func myTest1( num1:Int , num2:Int ) -> String{    return String(num1 + num2)}

1.3 external parameters (equivalent to an alias for the parameter for easy identification and memory)

Func myTest3 (myNum1 num1: Int, myNum2 num2: Int) {println (num1 + num2)} myTest3 (myNum1: 1, myNum2: 2) // call func myTest4 (# num1: int, # num2: Int) {// Add # above to directly use the form parameter name as the External Parameter Name println (num1 + num2)} myTest4 (num1: 2, num2: 3)

1.4 default parameters. (similar to C ++)

Func myTest5 (num1: Int, num2: Int = 10) {println (num1 + num2)} myTest5 (1) myTest5 (1, num2: 22) // The next parameter defaults to func myTest6 (num1: Int, _ num2: Int = 10) with the form parameter as the External Parameter Name. {// ignore the External Parameter Name of the form parameter, add _ println (num1 + num2)} myTest6 (1, 22) before the Parameter)

1.5 constants and variable parameters. (by default, Swift parameters are constant parameters and cannot be modified)

// This concept does not exist in other languages, such as C. you can modify func myTest7 (let num1: Int, num2: Int) {// num2 = 10 // This sentence is incorrect. You cannot modify println (num1 + num2 )} func myTest8 (var num1: Int, num2: Int) {// Add var to change it to a variable parameter. you can modify num1 = 10 println (num1 + num2 )}

1.6 input and output parameters inout (function similar to pointer in C language)

// Classic exchange function func swap (inout num1: Int, inout num2: Int) {// Add inout before the parameter to var tmp = num1 num1 = num2 num2 = tmp} var n1 = 10; var n2 = 11; swap (& n1, & n2) // Add the address acquisition symbol like C Language & println ("n1 = \ (n1), n2 = \ (n2 )")

1.7 function type. (The function is also regarded as a data type. It can be used as a parameter, a variable, or a return value)

// Note: if you do not specify a type when defining a variable, the type is automatically determined by default. when using this variable again, be sure to pay attention to the type matching problem // For example, the type of myTest1 in the above 1.2 function is (Int, Int) -> Stringvar myFun1 = myTest1 // The variable myFun type is (Int, Int)-> StringmyFun1 (10, 10) // The result is 20 func myFun2 (fun :( Int, Int) -> String, num1: Int, num2: Int) {// function as the parameter println (fun (num1, num2)} myFun2 (myFun1, 11, 11) // likewise, functions can also be used as return values.

1.8 function overload (similar to Java, but unrelated to the return value in Java)

Func testOverload (num1: Int) {println ("... ")} func testOverload (num1: Int, num2: Int)-> Double {println ("... ") return 0.1} func testOverload (num1: Int, num2: Int)-> Int {println ("... ") return num1 + num2} testOverload (10) var dou: Double = testOverload () // you must find a specific type of receipt to uniquely identify

1.9 nested functions. (The scope of nested functions is limited to internal functions)

Func doSomething (num: Int) {func walk () {println ("working! ")} Func eat () {println (" eating ")} if num = 1 {walk ()} else {eat ()} doSomething (1) doSomething (2) // eat () // cannot be called directly outside


Ii. Assertions

Similar to C, there is nothing to say

var a = 10assert(a>5, "Right")

Iii. Enumeration

It will not be assigned 0, 1 by default, as in C...

3.1 enumeration format

Enum CompassPoint {case North // case indicates that the new row of members is defined as case South case East case West case A, B, C // multiple members can also appear on the same row}

3.2 access Enumeration

Var direct = CompassPoint. Northdirect =. South // because the previous access is made, it is automatically inferred later. Do not write the enumeration name // CompassPoint.

3.3 enumeration combined with Switch

switch direct{case .North:    println("Lots of planets have a north")case .South:    println("Watch out for penguins")case .East:    println("Where the sun rises")case .West:    println("Where the skies are blue")default:    println("OK")}

3.4 Association enumeration. value that can be associated with change

Enum Barcode {case UPCA (Int, Int, Int) // It is associated with three parameters: case QRCode (String) // It is associated with a String} // var productBarcode = Barcode. UPCA (0, 1, 2) // associates with three numbers.

3.5 enumerate original values.

Enum MyASCII: Character {// defines the 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 println (Planet. venus. toRaw () // The value is 2. This auto increment // use fromRaw to find the original enumerated member println (Planet. fromRaw (2) // The returned data type is optional, meaning it does not necessarily exist.

Rating: New features are added for old things, simplifying the writing of functions in OC. It is not suitable for those who are used to OC.

Refer:

The Swift Programming Language

Apple Dev Center


Reprinted please indicate the source: http://blog.csdn.net/xn4545945




An enumeration function problem

You can think of enumeration as an integer.
Enum sortType {No, Name, Score}

Void sort (sortType type, Data data)
{
Switch (type)
{
Case No:
// Sort by student ID
Break;
Case Name:
Break;
Case Score:
Break;
}
}

The enumeration type is a function.

Enumeration type is not a function, it is a defined type. If you learn C ++, you should know that this is a Class-like thing. When you learn C, there are also struct. They are similar and can be understood as a data type. just as the float type includes the integer part and the decimal part, the integer part and the decimal part are independent values, but they must be combined to form the float type. This is a truth or a bit distorted, that is, you are like an array. it contains multiple array elements, but they can be collectively referred to as an array. I don't know this explanation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.