//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