One: Tuples
A format variable or a constant keyword tuple variable = (variable, variable, ...) Description: 1: The tuple variable is also a variable, but behaves differently from other variables
2: () parentheses can be placed into the N variable composition
Example
Import Foundation/*a is a tuple variable, () which is put in shape and string tuple type for shaping and string composition */var a = (1, "swift")//tuple variable A consists of shaping and string composition println (a)
Run results
(1, Swift)
Mport Foundationvar B = +/* Defines a tuple, the tuple variable is C, and () inside the variable B and string is placed. Tuple types have shaping and string composition */var C = (b, "Swift")//tuple variable B consists of shaping and string composition println (c)
Run results
(Swift)
Ganso access Mode
1: anonymous meta-group variable to access
Description
1.1 Anonymous tuple variables and well-defined tuple variable parameters must be consistent
1.2 Requires individual values in a tuple and can be used (_) to ignore unwanted values
Example (1.1)
Import Foundationvar A = (1, "Swift")//tuple variable A consists of shaping and string/*1:a is a tuple variable, consisting of shaping and string 2: (B,C) is an anonymous tuple variable 3: The value corresponding to the tuple variable A. One by one assigns a value to an anonymous variable. That is, B =1 C = "Swift" Note points: The number of anonymous tuple variables defined must be the same as the number of defined tuple variable values */var (b,c) = Aprintln ("b=\ (B), c=\ (C)")
Run results
B=1,c=swift
Example (1.2)
Import Foundationvar A = (1, "Swift")//tuple variable A consists of shaping and string/*1:a is a tuple variable, composed of shaping and string 2: (_,C) is an anonymous tuple variable, where x is underlined (_) Represents the value of the omitted variable 3: Tuple variable A, which assigns a value to an anonymous variable. That is b= "swfit" Note: The number of anonymous tuple variables defined must be the same as the number of defined tuple variable values */var (_,b) = Aprintln ("b=\ (B)")
Operation Result:
B=swift
2: Access by index (similar to array access)
Import Foundationvar A = (1, "Swift")//tuple variable A consists of shaping and string composition println (a.0)//a.0 refers to the first element in the access tuple variable that is 1println (A.1)//A.1 refers to an interview Ask the second element in the meta-ancestor variable, "Swift"
Run results
1Swift
3: Access via key
Note the point:
The premise is
1: Variables in a tuple variable must be accessed by a dictionary
2: The Access method is: The tuple variable. Key
Import foundation/* access the tuple variable via key Note 1: The corresponding variable must be dictionary key:value2: When accessing the tuple variable. Key*/var A = (frist: "Hello", second: "Swift") println (a.frist) println (A.second)
Run results
Helloswift
Two: type conversion
1: String conversion to Shaping (ToInt ())
Import foundation//string to int type var stra = "123" var value = stra.toint ()//ToInt function is to convert string to reshape println ( "Value=\ (value)") var StrB = "12A" var value1 = strb.toint ()//toint function is to convert a string into shaping where the conversion failure value is Nilprintln ("value1=\ (value1)")
Run results
Value=123value1=nil
var B = a.toint () This function string is converted to plastic to look at a
Where B is Int? (optional values) can contain shaped values and nil
var a:int = 10var a:int? = 10 different points: var a:int? = 10 can include not only the shaping value, but also the nilvar A:int = 10 can contain only the shaping value
2: Floating-point type conversion shaping (INT ())
Import Foundationvar A = 1.10 var B = Int (A)//cast floating-point type to shaping println ("b=\ (B)")
Run results
B=1
3: Reshape to floating-point type (Double ())
Import Foundationvar A = 1 var B = Double (a)//shaping converted to floating-point type var C = float (a) println ("B=\ (B) c=\ (C)")
Run results
b=1.0 c=1.0
Conditional statement Usage (IF)
The first of these methods
If Boolean {} Note point 1: Boolean equals True to execute if method inside 2:{} cannot be omitted otherwise the second method will be error
If optional value {} Note point: 1: Optional value is not equal to nil to execute if Method 2: {} cannot be omitted or error will be
The third method of
If let constant value = optional value {} Note: 1:if lets constant value = optional value assigns the optional value to the constant value, if the constant value is not nil, execute the If Method 2: {} cannot omit
or it will be an error.
Example
First method (if + Boolean)
Import Foundationvar a = true//if followed by a bool value of TRUE if the method after {} is executed if a { println ("a=\ (A)")}else{ println ("a")}
Run results
A=true
Second method (if + optional value)
Import foundation//-----------------------------The optional value is not nil----------------var a:int? = 10//a type Int? (optional type), If a is not nil then execute the IF in {} method if a { println ("a=\ (A)")}else{ println ("A=nil")}//--------------------------- Can I choose Nil---------------var b:int? b is an optional type, no assignment defaults to nil if B is nil do not execute if {} in the If B { println ("b=\ (B)")}else { println ("B=nil")}
Run results
A=10b=nil
Third Chinese law (if let constant name = optional value)
Import foundation//---------------If let optional value is not Nilvar a:int? = 10if Lets B = a{//Interpret a value assigned to B if B does not perform if for nil {} in Method println ("b=\ (B)")}else{ println ("B=nil")}//---------------if let optional value is Nilvar c:int?//c is an optional value that is not assigned by default to nilif let D = c {//interpret the value of C assigned to D if D is nil do not execute if {} in method println ("d=\ (D)")}else{ println ("D=nil" )}
Run results
B=10d=nil
I have come back to learn from the Swift language of the knowledge to write to form a series. As a new language, personal understanding inevitably has shortcomings, welcome to give me advice. can also add my QQ 14,360,511,081 discussion, if you have any problem, you can also directly in QQ message sent to me, I saw the first time after you reply
Finally, a summary. Send the mind map as the end of the article
Introduction to Swift-Basic type (3)