Swift Notes (iv)--collection type

Source: Internet
Author: User

collection typeSwift provides two types of collections, Array, Dictionaryarray and dictionaryneed to have the same typeof data//Actual test found can be mixed type, the following examplesArray is ordered, Dictionary is unordered Dictionary need a set of no unique index for the lookup, that is, key
ArrayPrototype: Array<sometype>, SomeType refers to the type of data to be placed in an Array
var myarray:string[] = ["Var1", "var2", "VAR3"] can be abbreviated as: var myArray = ["Var1", "var2", "VAR3"]
Assuming that the type of array is given. Then the array can only have a specific type, but the assumption is abbreviated form. Mixed data types can actually!

Until now, the official document has not mentioned such a feature: var myArray = ["string1", 1, 2]//xcode is able to show the correct results. Maybe the chapters are introduced.
Also capable of initializing an empty arrayvar myints = int[] ()
Or start all values with the same value var MyDouble = double[] (Count:3,Repeatedvalue:0.0)
Method cout (Gets the number of elements in the array) Count (Gets the number of elements in the array)(thanks to Swift Technical Exchange first platform (355277) group Friends of the Day (1651150754) pointed out typos)
Myarray.count
IsEmpty(bool type return value, used to infer that the array is not empty) myarray.isempty
Append(append element to array, at last) myarray.append ("Hello")
+ = (this type append. But more bull, able to connect two arrays.//Thanks to swift Apple Super Group (191026105) group Friends Shawn Shoper (369201170)+=(This is similar to append, but more bull, able to connect two arrays together)

MyArray + = "World" MyArray + = ["Hello", "kitty"]myarray + = MyArraynote, however, that the myarry of the "Promiscuous data type" We created just now, is that ["string1", 1, 2] is not able to do this, the editor will error: Could not find a overload for "+ =" that accepts The supplied arguments
Insert (atindex:)(insert element in specified subscript) Myarray.insert ("Need",Atindex:0)//This is the continuation of the Objective-c .in an out-of-bounds subscript position the insertion is not agreed, because the array is contiguous
Removeatindex (index:)(Delete the element that specifies the subscript position) Myarray.removeatindex (0)
Removelast(delete last Element) Myarray.removelast ()
RemoveAll (keepcapacity:)(Delete all elements) Myarray.removeall (Keepcapacity:false)
There's another way to remove all the elements: MyArray = []//Obviously such a method is very useful. But with RemoveAll, which is fast, it needs to be measured in the program.
under the banner Law (subscript syntax)var Myfirstitem = MyArray[0]//Subscript starts from 0
It is also possible to use the range-type subscript.

。。 Good ray: Myarray[2 ... 3] = ["China", "Shanghai"] it is noteworthy that. It's used here. That is, the subscript is 2,3,4, and the number of assignments can be different, here are two cases need to explain: 1. The number of subscripts is more than the new value: very good explanation.  Throw out 2 more. The number of subscripts is less than the new value: wonderful. This array will be inserted at the given maximum subscript at the place where the extra value is located. And the value that was in the back will move backwards.
This, of course, refers to the array of variables. Instead of a constant array
Out of boundsThe cross-border will still collapse ... That's not much to say.
examples1. For-infor item in MyArray {println (item)}
2. Enumerate this is a global function for (index, value) in enumerate (MyArray) {println ("Item \ (index + 1): \ (Value)")}
DictionaryKey to be unique, assuming that an existing key operation, will be rid of the original value and Nsdictionary and Nsmutabledictionary is different, dictionary can use whatever object as key and value
Prototype: Dictionary<keytype, valuetype>//According to the description of the official document, it is possible to know that Dictionary is a basic data type for all swift of the table. String, Int, Double, Bool all can be key
Initialization method similar to array: var mydictionary:dictionary<string, string> = ["China": "Panda", "Australia": "Kangaroo"]
Because Swift can infer data types based on numeric values, dictionary can be initialized without specifying type: var mydictionary = ["China": "Panda", "Australia": "Kangaroo"]
Similar to array. Can also create an empty dictionary:var MyDictionary = dictionary<string, string> ()
Empty the elements in the dictionary: mydictionary =[:]          //Note the colon here
MethodCount(Get the number of key-value pairs in dictionary) mydictionary.count
Updatevalue (forkey:)(Change the value of a key) Mydictionary.updatevalue ("Duckbill",Forkey:"Australia")
Can also be written as MyDictionary["Australia"]= "Duckbill"
examplesfor-inFor (country, animal) in MyDictionary {println ("Counter: \ (Country), animal: \ (Animal)")}
can also be individually keyfor country in mydictionary.Keys{...}
Separate examples valuefor animal in mydictionary.Values{...}
can also take key or value out to create a arrayvar Mykeyarray = Array (mydictionary.keys) var myvaluearray = Array (mydictionary.values)
fixed length and indefinite lengthOfficial documents called mutable and immutable (variable, immutable) I think there is a message hidden here: Not the element cannot be changed, but the length is immutable, for example let MyArray = [three-way]//This array can only have 3 elements, can not be more or less, But can change the value inside let mydictionary = [1: "1", 2: "2"]//similar to array

Swift Notes (iv)--collection type

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.