http://blog.csdn.net/kuloveyouwei/article/details/36005299
Swift provides two collection types to store collections, arrays, and dictionaries. An array is a collection of serialized lists of the same type. A dictionary is a non-serialized collection that can use a unique identifier similar to a key to get a value. In Swift, both the key and the value of the array and dictionary must be explicit about its type. This means that the array and the dictionary do not insert a value of the wrong type, resulting in an error. This also means that when you retrieve a value in an array and a dictionary, you can determine its type. Swift uses the identified collection types to ensure that the code works without errors, and allows you to catch errors earlier in the development phase.
1. Arrays
The swift array is a stored determined value, and the Nsarray and Nsmutablearray classes in this objective-c are distinguished. Because they are stored in various objects, and do not provide specific information to return any relevant objects. In Swift, whether it is a deterministic declaration or an implicit declaration, the array is very certain what type it is stored on itself, and it does not necessarily require that the class object be stored. So the swift array is type-safe because it always determines the values it can contain.
[OBJC]View Plaincopy
- Initializing an array
- Let array1:string[]=["A","B"];
- An array is defined as a variable (using the var identifier) instead of a constant (using the Let identifier)
- var array2:string[]=["AA","BB"];
- Initialize an empty array
- var array3=string[] ();
- The SWIFT array type also provides an initialization method to create an array that determines the length and provides a default value. You can add a new array by this initialization method, the number of elements becomes count, and the appropriate default value is Repeatedvalue
- var array4=int[] (count:3,repeatedvalue:0);
- Array2 variable array, you can add elements
- Array2. Append ("cc");
- You can also add elements by using the + = operator
- array2+= "CC";
- println (array2);
- /*
- * Print Results
- *[AA, BB, CC]
- */
- Index value by subscript method
- var yuansu1=array2[0];
- println (Yuansu1);
- /*
- * Print Results
- *aa
- */
- You can also modify the values in a range
- Array2[1 ... 2]=["ee","FF"];
- println (array2);
- /*
- * Print Results
- *[AA, EE, FF]
- */
- Inserting an element at a location in an array
- Array2. Insert ("oo", Atindex:0);
- println (array2);
- /*
- * Print Results
- *[OO,AA, EE, FF]
- */
- Delete an element at a location
- Array2. Removeatindex (0);
- println (array2);
- /*
- * Print Results
- *[AA, EE, FF]
- */
- Move the last element of the divisor group
- var lastyuansu=array2. Removelast ();
- Iterating through an array
- For item in array2
- {
- println (item);
- }
2. Dictionaries
The Swift dictionary stores a type of specific keys and values, and the Objective-c nsdictionary and nsmutabledictionary are distinguished by certain differences, because they are used by various objects as their keys and values, And does not provide any specific information about the object. In Swift, for a particular dictionary, the keys and values that it can store are deterministic, either explicitly declared or implicitly inferred types. Swift's dictionary notation is that Dictionary<keytype,valuetype>,keytype is the key you want to store, and ValueType is the value you want to store. The only limitation is that KeyType must be hashed (hashable)--that is, providing a form that allows them to be independently identified. All of Swift's underlying types (such as String, shape (Int), double-precision (double) and Boolean (BOOL)) are hashed by default (hashable), and these types are often used as keys to the dictionary. Enumeration member values do not require a assistance value (associated values) (specifically described in enumerations) as well because they are also hashed (hashable) by default.
[OBJC]View Plaincopy
- Initialize a dictionary
- Let dict1:dictionary<string,string>=[' Name ':' Yu ',' age ':' 26 '];
- As with arrays, if you initialize a dictionary with the same type, you can not specify the type of dictionary.
- var dict2:dictionary<string,string>=["Name2":"Yu","Age2":"26"];
- Create an empty Dictionary
- var dict3=dictionary<string,string> ();
- Subscript method, when there is no this key, is a new element
- Dict2["Sex"]="Nan";
- /*
- * Print Results
- *[name2:yu, age2:26, Sex:nan]
- */
- Modify
- Dict2["Sex"]="n";
- println (dict2);
- /*
- * Print Results
- *[name2:yu, age2:26, Sex:n]
- */
- Similarly, use the dictionary updatevalue (forkey:) method to set or update the value of a particular key. As with the subscript example above, Updatevalue (Forkey:) method sets its value if the key does not exist, updates its value if the key exists, and Updatevalue (Forkey:). method if it is updated, the original old value is returned rthis enables you can use this to determine if an update has occurred.
- If let OldValue = Dict2. Updatevalue ("Wang", forkey: "Name2")
- {
- println (dict2);
- }
- /*
- * Print Results
- *[name2:wang, age2:26, Sex:n]
- */
- The value is obtained by subscript method
- If let value = Dict2["Age2"]
- {
- println (value);
- } Else
- {
- println ("no Exsit")
- }
- You can use the following banner to assign his value to nil to remove the key value pair.
- Dict2["Sex"]=Nil;
- println (dict2);
- /*
- * Print Results
- *[name2:yu, age2:26]
- */
- Similarly, removing a key-value pair from a dictionary can use the Removevalueforkey method, which, if there is a value corresponding to the key, removes a key-value pair and returns the removed value, otherwise nil.
- If let Removedvalue = Dict2. Removevalueforkey ("Age2")
- {
- println (dict2);
- } Else
- {
- println (dict2);
- }
- Traverse Dictionary
- for (Key,value) in Dict2
- {
- println ("\ (key): \ (value)");
- }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Swift learns three