Swift-5 collection type

Source: Internet
Author: User

//Playground-noun:a Place where people can playImport Uikitvar str="Hello, playground."//Swift's collection type has only arrays and dictionaries//only data of the same type can be stored in an array (either a base type or a class object)//declaration method [String] array<string>//once implicit is declared, his type is determined, and no other type of data can be storedvar array=["a","b"]var Array1:[string]=["a","b"]var Array2:array<string>=["a","b"]array[0]="AAA"Arrayvar Array4=[Int] () varstring=String () var array5=array<string>()//This will not be an error, but there is no specific type, it defaults to the Nsarray type in OC array, so the definition of the array has lost its meaning, can not do too much operation//var array6=[]//array6.append (3)//error: ' Nsarray ' does not has a member named ' Append '//Empty ArrayArray1=[]//emptying does not change the type of data it can storeArray1.append ("CCC") Array1=[String] () array1=array<string>()//Create and initializevar Array6=[int] (count:Ten, Repeatedvalue:0) Array6.append ( -) Array6//Merging Arraysvar array7=[1,2,3]var Array8=array6 +Array7array8array8+= [4,5]println (Array8) Array8.removeatindex ( -)//Insert, Romove method is almost the same as otherarray8.removelast ()//Note that index is internal to the indexed value//Batch replacement, note is three dot numberarray8[2...4] = [2,2,2]array8array8[2...4]=[ -]//no need to match the number of elements to the number of modificationsArray8//array8[2...4]=0//It's wrong to write like this. forIndexinch 0... array.count{println (index)} forItemincharray{println (item)}//using the Enumerate function for(Index,item)inchEnumerate (array) {println ("\ (Index)-\ (item)")}//Dictionarys dictionaries are unordered.var dictionarys=[1:"a",2:"b"]//Implicit declaration//stored keys and values can be of any type//A dictionary can only store fixed one key and value data type collocation (!!). Particularly important)//dictionary<int,string>//Show Claims//Note that the separator is a number instead of:var dict1:dictionary<int,string>=[1:"BBB"] println (dict1)//here split is: No, No.var dict2:[int:string]=[1:"AAA"]//var dict3:[int, string]=[1: "AAA"]//Errorprintln (DICT2)//Empty Dictionaryvar dict3=dictionary<int,string>() var dict4=[int:string] ()//easy way to empty the dictionarydict2=[:]//also emptying does not change the type of data that a dictionary can storedictionarys.countdictionarys.isemptydictionarys[1]//selectable data,//"Hahha" +dictionarys[1]//wrong wording"Hahha"+dictionarys[1]!//correct//key is non-repeatable//Insert New Valuedictionarys[3]="C"  //Insert no corresponding new key and valueprintln (Dictionarys)//Modifydictionarys[3]="3c"var oldValue=dictionarys.updatevalue ("3CCC", Forkey:3)//Deletedictionarys[3]=nilprintln (Dictionarys) Dictionarys.removevalueforkey (2)//Returns the deleted value with the return value//Traverse Dictionary  for(Key,value)inchDictionarys {println ("\ (key): \ (value)")} Dictionarys.keys dictionarys.values forKeyinchDictionarys.keys {println (key)} forValueinchdictionarys.values {println (value)}//A let array or dictionary definition is not allowed to have any change operation.

Swift-5 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.