One: Array
One: variable array definition: Arrays use ordered lists to store multiple data of the same type. format: The first format of the var variable: type [] = [variable value, variable value,...] The second format var variable =[variable value, variable value,...] Description: 1:[variable value, variable value ...]
Note the 1: constant keyword (let) defines an array of immutable variables that cannot be modified and can only be accessed. 2: Variable keyword (var) defines an array that can be modified to be accessed by variable groups.
Example
-------the definition of the array------------/*1:["Hello", "Swift"]-->[] contains the variable value type is a string, that is, the string variable array 2; compiler through ["Hello", "Swift"] To infer that the type of the ARR variable is a string variable array type */var arr = ["Hello", "Swift"]/*1:var arr1:string[]---> Direct definition arr1 is a string variable array type, The values in its array must be string */var arr1:string[] = ["Hello", "Swift"]println ("arr =\ (arr), arr1=\ (arr1)") Run result arr =[ Hello, Swift], Arr1=[hello, Swift]
Watch out.
var str = ["Hellow", 11]println ("str =\ (str)") Run results str = ( "Hellow", one-to-one )/*1:["Hellow", one-by-one]-->[] Variable value types are string and shaped, type inconsistent, that is, not array 2: result ("Hellow", 11) is an immutable group */
Operation of a variable group
①: Array length and number of variable value modifications
-----------the length of the array----var Arr = ["Hell0", "Swift"]var count = Arr.count//count property returns the length of the array println ("Count=\ ( count)//------------the variable value in the array------var str = [81,82,85]str[2] =//str[2] refers to the variable value in the array indexed to 2 Re-assignment to 83println ("str=\ (str)") Run Result: count=2str=[81, 82, 83]
②: Append + = Append for variable arrays
Append---appped () function for-------array------------var arr = [up]//define shaping array variable arrarr.append (3)//append (3) is to append the variable value 3 in the end of the ARR array to the array, that is, the array length is increased by 1println ("arr = \ (arr) length =\ (arr.count)")//-------Array appended---+ =------------var arr1 = [10,11]//define shaped array variable arrarr1 + = 12//+ = is the append variable value 3 to the array in the end of the ARR array 1 (the internal implementation principle will also use the Append function) println ("Arr1= \ (arr1 ) length =\ (arr1.count) ") operation result arr = [1, 2, 3] length =3 arr1= [10, 11, 12] length =3
③: Inserting insert ( variable value , Atindex: Index) of a mutable array
Append---Insert (variable value, Atindex: index) for-------array-----------var arr2 = [10,11]arr2.insert (9,atindex:0)// Insert variable value in array arr 0 9 array length plus 1PRINTLN ("arr2=\ (arr2) length =\ (arr2.count)") Run result arr2=[9, 10, 11] length =3
3: Variable array removalremoveAtIndex(索引),
removeLast()
Remove the Removelast () array--------------the end of the array removes the variable value----var ARR4 = [200,201,202]arr4.removelast ()///shift the value of the trailing end of the divisor group ARR3, Array length minus 1println ("Arr4 =\ (ARR4) length =\ (arr4.count)")//--------------array removal Removeatindex (Index) array specifies the location to remove the variable value----var arr3 = [100,101,102]arr3.removeatindex (0)//The value of the variable indexed to 0 in the divisor group ARR3, the array length minus 1PRINTLN (" ARR3 =\ (ARR3) length =\ (arr3.count) ") Run result ARR4 =[200, 201] Length =2ARR3 =[101, 102] length =2
4: Create an empty array
①: Defining a variable empty array
Format: variable keyword variable = Type [] () Example: var arr = int[] () Note points: 1: constant keyword (let) the array of rhetoric is an immutable group, cannot be modified, defines an empty character array without meaning 2: variable keyword (var) The array of rhetoric is a mutable array that can be dynamically changed.
②:isempty function to determine if an array is empty
-----------empty array----/*1:int[] () is a shaped empty array 2:arr array variable is let rhetoric, arr array can only access */let arr = int[] ()/*1:int[] () is an empty array of shapes 2:arr1 Array variables are var rhetoric, arr array is variable array, can be dynamically modified */var arr1 = int[] () arr1 + = 1println ("arr1= \ (arr1)")//-----------Determine if the array is empty---var ARR2 = int[] () If Arr2.isempty {//isempth is to determine if the array is an empty array println ("arr2 array is empty")}else{ println ("arr2 array is not empty")} Run result arr1= [1]arr2 array is empty
The traversal of the array for in (previous article has spoken about usage)
Access to------------array--for in -----var arr = ["Hello", "Swift"]for str in arr { // for variable in collection when executing for in when the value of the variable in the set is assigned to STR println (str)} Run Result: Helloswift
Two: Variable dictionary
A: a mutable dictionary definition: A dictionary is a memory that stores multiple data of the same type. Each value is associated with a unique key (key), which is used as the identifier format for this value data in the dictionary : the first format variable variable: dictionary< type, type > = [variable Value: variable value, variable value: variable value, ...] The second format variable variable =[variable value: Variable value, variable value: Variable value,...] Description: Example: First format: var dic:Dictionary<String ,Int>= ["H1": 1, "H2": 2]
The second format: var dic = ["H1": 1, "H2": 2] Description: Dictionary specification 1:[] consists of Key:value key values
Note: 1: The constant keyword (let) defines a dictionary that is not a dictionary and is not able to make any modifications that can be accessed only. 2: The variable keyword (var) defines the dictionary, which is a mutable dictionary that can be modified and can be accessed.
Example:
/*1: [] There are key:value composition 2: All key types have been and do not repeat, all value types are consistent with 3: that is ["H1": 1, "H2": 2] is a dictionary type, the compiler deduced through the dictionary type dic is a variable dictionary */var dic = [" H1 ": 1," H2 ": 2]println (" dic=\ (DIC) ")/*1:var dic1:dictionary<string,int> Direct definition variable dictionary type key is string value Characters in the 2:key cannot be duplicated */var dic1:dictionary<string,int> = ["H1": 1, "H2": 2]println ("dic1=\ (DIC1)") Run result dic=[h1:1, H2:2 ]dic1=[h1:1, H2:2]
Note the point:
/*1:[] is a key:value composed 2:key type has been, not duplicated, but the value type is inconsistent that is immutable dictionary */var str = ["str": 1, "hell0": "Swift"]println (str)
Variable Dictionary modification values
1:updatevalue (change value, Forkey key value)
2: Modify value by [key]
-------------to modify the value---updatevalue (modify value, Forkey key value) by key/*1:[] is a key:value composition 2:key type consistent, not fully value Type consistent is a mutable dictionary */var str = ["str": "AA", "hell0": "Swift"]str.updatevalue ("BB", Forkey: "str") println ("str =\ (str)")//- ------------modify value by key---Same as [key] to modify values var str1 = ["str": "AA", "hell0": "Swift"]str1["str"] = "CC"// Dictionary with key (str) directly modifies key corresponding to Valueprintln ("str1=\ (STR1)") Running result: Str =[str:bb, HELL0:SWIFT]STR1=[STR:CC, Hell0:swift]
Variable dictionary-The value that is obtained by key is an optional value.
/*1:[] is a key:value composed 2:key type consistent, not fully value type consistent is a mutable dictionary */var str = ["str": "AA", "hell0": "Swift"]var temp = str["str"]< c6/>//Access dictionary does not exist key, return value is Nilvar temp1 = str["str"]//Access dictionary exists key, return corresponding VALUEPRINTLN ("temp=\ (temp), Temp1 = \ (TEMP1)")//Summary Str[key] The return value may be correct, or it is possible to return nil that is Str[key] return value is an optional value run result; TEMP=NIL,TEMP1 = AA
The traversal of the dictionary----for in
var dic = ["Hello": 1, "Swift": 2]// for tuple in dictionary collection uses tuples to represent the corresponding key values in the dictionary for (key,value) in dic { println ("key=\ (Key), value=\ (Value)")}
Create an empty mutable dictionary
Format: var variable = dictionary<key type, value type ()
Create a key for the string value for the shaped empty dictionary, and the dictionary for the mutable dictionary var arr = dictionary<string,int> ()
Introduction to Swift-collection