swift-4-Arrays and dictionaries

Source: Internet
Author: User

//Playground-noun:a Place where people can playImport UIKit//Array Dictionary//The set of variables assigned to the Var set is a mutable mutable, and the set assigned to let is immutable immutable//array array<sometype> equivalent to [SomeType]var shoppinglist: [String] = ["Eggs","Milk"]//var shoppinglist = ["Eggs", "Milk"] Auto infer type//Array Countprintln"The shopping list contains \ (shoppinglist.count) Items")//checks if the array is emptyifshoppinglist.isempty {println ("The shopping list is empty")} Else{println ("The shopping list is not empty")}//adding elements to an arrayShoppinglist.append ("Flour") Shoppinglist+= ["Baking Powder"]//accessing elements in an arrayvar FirstItem = shoppinglist[0]//The number of elements in the bulk substitution array for substitution is not necessarily the same as the range of replacementsshoppinglist[1...2] = ["Apples"]//inserting elementsShoppinglist.insert ("Maple Syrup", Atindex:0)//removing elements from an arrayLet Maplesyrup = Shoppinglist.removeatindex (0) Shoppinglist.removerange (Range (Start:1, End:2) ) Shoppinglist.removelast ()//iterating through an array forIteminchshoppinglist {println (item)} for(Index, item)inchEnumerate (shoppinglist) {println ("Item \ (index + 1): \ (item)")}//Create and initialize an arrayvar someints =[Int] () println ("someints is of type [int.] with \ (someints.count) Items")//assigning default values when creating arraysvar thressdoubles = [Double] (count:5, Repeatedvalue:0.0)//Combining Arraysvar anotherdoubles = [Double] (count:3, Repeatedvalue:0.1) var eightdouble= Thressdoubles +Anotherdoubles//Dictionary//Dictionary<key, value> [Key:value]var airports: [String:string] = ["YYZ":"Toronto Pearson","DUB":"Dublin"]//var airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]//accessing, modifying dictionariesprintln"The airports Dictionary contains \ (airports.count) Items")ifairports.isempty {println ("The airports Dictionary is empty")} Else{println ("The Airports Dictionary is not empty")}//adding elementsairports["LHR"] ="London"airports["LHR"] ="London Heathrow"//The key value is set by method. Returns the replaced value optionalLet OldValue = Airports.updatevalue ("Dublin Airport", Forkey:"DUB")//removing key-value pairsairports["DUB"] =Nilairports.removevalueforkey ("DUB")//Traverse for(Key, value)inchAirports {println ("key: \ (key), Value: \ (value)")}

swift-4-Arrays and dictionaries

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.