Swift, like other languages, offers two collection types: arrays and dictionaries
Arrays: arrays are used to store the same type of data sequentially, and Swift specifies that it is type-safe, and that each array has its own type, which is what other languages call generics.
To create an array:
1. var aa:[string] = ["AA", "BB", "CC"] or var AA = ["AA", "BB", "CC"]
2. var aa = [String] ()//Empty array aa.append ("AA")//Add a member to AA array, aa=[]//empty array
3, var a = [Double] (count:5,repeatedvalue:0.0)//Create an array of 5 sizes, and are assigned a value of 0.0, due to the characteristics of the swift type inference, you can also not specify the type,var a = Array (coun T:5,repeatedvalue:0.0) but not recommended.
4.var a = [Double] (Count:5,repeatedvalue:0.0); var b = [Double] (Count:5,repeatedvalue:5.5); var C = a + B;//combines two array items into an array, and the type is inferred from the added array.
Access and modification of the array:
An array has operations such as adding items, modifying items, deleting items, traversing items, and so on.
var AA = [String] ()
Determine if the array is empty, aa.isempty ()
Add to end of array: Aa.append ("test") or AA + = ["test1"] or aa+=["test2", "test3"]
Insert to a location: Aa.insert ("Test2", atindex:0)
Get an item: subscript var tt = aa[0]
Modified: aa[0] = "New test" can also be bulk modified aa[4...6] = ["A", "B"]
Delete item: var RemoveItem = aa.removeatindex (0) The removed item is returned. Delete last var RemoveItem = Aa.removelast ()
The traversal can be used for the in loop. This to the for loop when the discussion. From the Append method above, you can see that the array is mutable. Of course, defined as a let type is immutable ...
A dictionary, which is a key-value pair. In order to store dictionary<keytype, the only limitation of valuetype> KeyType is the hash, which guarantees that it is unique, all Swift basic types (such as String,int, Double and bool) are the default hashes.
Created: 1, by literal creation of Var aa:dictionary<string, string> = ["a": "AAA", "B": "BBB"] of course can also omit Dictionary written as var aa = ["A": "AAA "," B ":" BBB "]
2. Create an empty dictionary var aa = dictionary<string, string> () empty the data AA = [:]//More than an array of one: number
Read:
1. Dictionary Size Aa.count Count property
2, subscript get var tmp = aa["a"]
Modify:
1, Subscript modified aa["a"] = "abc"//a this key exists is modified, does not exist is added. Very smart
2. Method modification var oldValue = aa.updatevalue ("BBCCDD", Forkey: "B")
Removed from
1. Remove with nil: aa["a"] = Nil to remove key a
2, Method removal: var oldValue = Aa.removeforkey ("a") a this key and the existing data is removed
Traversal is also used for the in loop. discussed in the For loop.
Unlike OC, which does not outsource a layer of object-oriented only on the basis of C, it is really flexible and very similar to other programming language grammars. It's easy to get started.
"Swift" Learning Notes (iv)--collection (Collection)