Swift offers three collection types
- Array (array) can be repeated in order []
- Set (set) is not sequential and cannot be repeated {}
- Dictionary (dictionary) key-value pair {}
Array (array) to create arrays
/** Create an empty array of type int */ var someints = [Int] (); /* add an element to the array 3*/ someints.append (3 ); /* the array to NULL */ someints = []; /* creates an array of type Double containing 3 values, each of which is 0.0*/ var treedoubles = [Double] (count: 3 , Repeatedvalue: 0.0 ); /**/var otherdoubles = [Double] (Count:4, repeatedvalue:1.6);/* Create a new array using two arrays of the same type */ var doublearray = treedoubles + otherdoubles; /* create an array directly */ var strarray = [ "Love Me" , "Don't Love Me" , "Love Me" , "Don't Love Me" ];
Accessing and modifying arrays
Strarray.count;/** Returns the length of the array 4*/Strarray.isempty;//Determine if the array is empty falseStrarray.append ("Go to hell.");//Add an element to the arrayStrarray + = ["Of course I don't love you."];//plus an arrayStrarray + = ["Husband","Wife"]; strarray[0];//access the first element of an arraystrarray[0] ="Tease";//Modify the value of the 1th elementstrarray[5... 7] = ["boring"];//Place the 5-7 element in this range to modify the bit "boring"Strarray.insert ("Child", Atindex:3);//Insert a string in the fourth positionStrarray.removeatindex (3);//Remove elements from the fourth positionStrarray.removelast ();//Remove last element
Iterating through an array
forstr in strArray { print(str);}
Use the enumerate () function
forvaluein strArray.enumerate() { print("\(index + 1) 个元素是 \(value)");}
Set (SET)
The values in the set type are not equal, and swift determines whether the two values are equal by two values that are worth hashcode.
Create set
Set<String>(); //创建一个空的SetstrSet.insert("one"); //往Set中插入一个值strSet = []; //把set置空var numSet: Set<String> = ["one", "two", "three","four"]; //直接声明
Note: The set type cannot be inferred, so be sure to give the set type when creating the set
var numSet = ["one""two""three","four"];//这样会被推断位数组var numSet:Set = ["one""two""three","four"];//这样才是Set,String类型会自动推断
Access Modify Set
numSet.count; //返回元素的个数numSet.isEmpty; //判断numSet是不是空numSet.insert("five" //往numSet中插入元素numSet.remove("two");/*移除set中的元素,如存在返回这个元素,否则返回nil*/numSet.contains("one");//判断set是否包含这个元素
Traversing set
forin numSet { print(num);}//返回排序好的set元素forin numSet.sort() { print(num);}
Find the intersection, set, complement, etc.
Let Set1:set = [1,2,3,4]; Let Set2:set = [3,4,5,6The];sort () function willSetConvert to ArraySet1.union (Set2). sort ();//and set [1,2,3,4,5,6]Set1.intersect (Set2);//Intersection {3,4}Set1.subtract (Set2);//Not inSetElement in 2 {2,1}Set1.exclusiveOr (Set2)//Not in the set of {5,6,2,1}
Include relationship
Let SetA:set = [1,2,3,4]; Let SetB:set = [3,4,5,6]; Let SetC:set = [1,2];SetC.issubsetof (SetA); //SetC is not included withSetAtrueSetA.issupersetof (SetC); //SetA is not includedSetCtrueSetA.isdisjointwith (SetB); //SetA is not a disagreementSetb intersectfalseSetC.isdisjointwith (SetB); //SetC is not a disagreementSetb intersectfalse
Dictionary (dictionary) Create a dictionary
/*创建一个空的字典*/var nameOfInteger = [Int:String]();/*添加一个元素*/nameOfInteger[1"one";/*把字典置空*/nameOfInteger = [:];//直接创建一个字典var strDict = ["one":"高富帅""two":"土豪""three":"帅哥""four":"屌丝"];
Accessing and modifying dictionaries
/*返回元素个数*/strDict.count;/*判断字典是不是空*/strDict.isEmpty;/*访问字典键位four的元素*/strDict["four"];/*修改这个元素,如果没有就是添加*/strDict["four""土鳖";/*更新元素,没有就添加然后返回nil*/let oldValue = strDict.updateValue("丑比""four");/*访问元素,没有就是nil*/letstr = strDict["three"];/*删除这个元素*/strDict["four"] = nil;
The traversal of a dictionary
for (key, value) in strDict { print(value);}/**遍历key*/for key in strDict.keys { print(strDict[key]);}//将key或value转为数组let keyArray = [String](strDict.keys);
"Swift Summary" collection