1. tuples
Format variable or constant keyword tuples variable = (variable, variable ,...) Note: 1: The tuple variable is still a variable, but the expression is different from other variables.
2: () The brackets can contain N variables.
Example;
Import Foundation/* A is A tuples variable. () contains an integer and A string. the type of the tuples is an integer and A string. */var A = (1, "swift ") // The tuples variable A consists of an integer and A string. println ()
Running result
(1, swift)
Mport Foundationvar B = 12/* defines a tuples. The tuples are C, and the integer values B and string are put in. The type of the tuples contains an integer and a string. */var C = (B, "Swift") // The variable B is composed of an integer and a string. println (C)
Running result
(12, Swift)
Yuanzu access method
1:Access using anonymous tuples
Note:
1.1 The parameters of the anonymous tuples and the defined tuples must be consistent.
1.2 you need to specify individual values in the tuples. You can use (_) to ignore unnecessary values.
Example (1.1)
Import Foundationvar A = (1, "Swift") // The tuple variable A is composed of an integer and A string./* 1: A is A tuple variable, consisting of an integer and A string. 2: (B, c) It is an anonymous tuples variable 3: values corresponding to the tuples variable A, which are assigned to anonymous variables one by one. B = 1 C = "Swift" Note: The number of defined anonymous tuples must be the same as the number of defined tuples */var (B, C) = Aprintln ("B = \ (B), C = \ (C )")
Running result
B=1,C=Swift
Example (1.2)
Import Foundationvar A = (1, "Swift") // The tuple variable A is composed of an integer and A string./* 1: A is A tuple variable, consisting of an integer and A string. 2 :(_, c) It is an anonymous tuples variable. The x underscore (_) indicates omitting the value of variable 3: The value corresponding to the tuple variable A, which is assigned to the anonymous variable one by one. Note: The number of defined anonymous tuples must be the same as the number of defined tuples */var (_, B) = Aprintln ("B = \ (B )")
Running result:
B=Swift
2: Access through indexes (similar to array access)
Import Foundationvar A = (1, "Swift") // The tuple variable A consists of an integer and A string. println (A.0) // A.0 indicates that the first element in the access tuples is 1 println (A.1) // A.1 indicates that the second element in the access element is "Swift"
Running result
1Swift
3: Access through key
Note:
Premise:
1: the variables in the tuples must be accessed by dictionaries.
2: Access Method: tuples variable. key
Import Foundation/* use key to access tuples. Note: 1: The corresponding variable must be the dictionary key: value2: when accessing, The tuples variable. key */var A = (frist: "Hello", second: "swift") println (. frist) println (. second)
Running result
Helloswift
Ii. type conversion
1: Convert string to INTEGER (toInt ())
Import Foundation // convert string to Int type var strA = "123" var value = strA. the toInt () // toInt function converts a string to an integer println ("value = \ (value)") var strB = "12A" var value1 = strB. the toInt () // toInt function converts a string to an integer. The Conversion failed value is nilprintln ("value1 = \ (value1 )")
Running result
value=123value1=nil
Var B = A. toInt () This function string is converted to an integer.
Where B is Int? (Optional) can contain integer value and nil
Var A: Int = 10var A: Int? = 10 Differences: var A: Int? = 10 can contain not only integer values, but also nilvar A: Int = 10 can only contain integer values
2: integer for floating point type conversion (Int ())
Import Foundationvar A = 1.10 var B = Int (A) // forcibly convert the floating point type to the integer println ("B = \ (B )")
Running result
B=1
3: integer to floating point type (Double ())
Import Foundationvar A = 1 var B = Double (A) // convert integer to floating point type var C = Float (A) println ("B = \ (B) C = \ (C )")
Running result
B=1.0 C=1.0
Conditional statement usage (if)
Method 1
If Boolean {} Note: 1: if the Boolean value is true, the method 2: {} in the if statement cannot be omitted. Otherwise, the second method is reported incorrectly.
If optional value {} Note: 1: if the optional value is not equal to nil, execute Method 2: {} in the if statement. Otherwise, an error is returned.
Method 3
If let constant value = optional value {} Note: 1: if let constant value = optional value is to assign the optional value to the constant value, if the constant value is not nil, the method 2: {} in the if statement cannot be omitted. Otherwise, an error is returned.
Example
Method 1 (if + Boolean)
Import Foundationvar A = true // when the bool value behind the if statement is true, run the {} method after the if statement if A {println ("A = \ ()")} else {println ("")}
Running result
A=true
Method 2 (if + optional)
Import Foundation // ------------------------------- the optional value is not nil ---------------- var A: Int? = 10 // the type of A int? (Optional). if A is not nil, execute the if A {println ("A = \ (A)")} method in if )")} else {println ("A = nil")} // ----------------------------- you can select nil ----------------- var B: Int? // B is optional. The default value is nil if B is nil. if B is nil, The PUT Method of {} In if is not executed if B {println ("B = \ (B )")} else {println ("B = nil ")}
Running result
A=10B=nil
Method 3 (if let constant name = optional value)
Import Foundation // ----------------- The optional value of if let is not nilvar A: Int? = 10if let B = A {// explain the value of A to B if B is not nil execute the if {} method println ("B = \ (B )")} else {println ("B = nil")} // --------------- if let, the optional value is nilvar C: Int? // C is an optional value, if no value is assigned, nilif let D = C {// The value of C is assigned to D. if D is nil, the method println ("D = \ (D) In if {} is not executed) ")} else {println (" D = nil ")}
Running result
B=10D=nil
I wrote the swift language I learned to form a series. As it is a new language, my personal understanding is inevitable. You are welcome to give me some comments. You can also add QQ 1436051108 for discussion. If you have any questions, you can also send a message to me via QQ. I will reply to you immediately after seeing it.
Summary. Send a mind map to end the article