Swift学习之十一:数组与字典(Array and Dictionary are collection types)

来源:互联网 发布:民国公元纪年法的算法 编辑:程序博客网 时间:2024/06/06 07:16
/* Swift 提供了两种集合类型,即数组(Array)和字典(Dictionary),存储值的集合   数组存储相同类型的顺序列表值。字典存储无序同类型值的集合,通过键来查询和   引用。   在Swift中,数组和字典总是值和键的存储类型是明确的。这意味着不能插入错误的类型到字典   和数组中。这种显示类型可以保证你的代码中值的类型总是明确的。   */// 数组类型全型为Array<SomeType>,也可以使用SomeType[]这种写法。虽然这两种类型是一样的,但// 后者更佳,并且都会使用后者。var shoppingList: String[] = ["Egg", "Milk"]// 访问和修改数组元素println("The shopping list contains \(shoppingList.cout) items")// 判断数组是否是空数组if shoppingList.isEmpty {  println("Empty array")} else {  println("Non empty array")}// 追加元素 可以使用append方法 ,可以使用+=shoppingList.append("Flour") // 变成:["Egg", "Milk", "Flour"]shoppingList += "Baking Power" // 变成:["Egg", "Milk", "Flour", "Baking Power"]// 通过下标访问元素var firstItem = shoppingList[0] // Egg// 通过下标修改元素shoppingList[0] = "Six eggs" // 把Egg变成了Six eggs// 通过范围下标修改shoppingList[1..3] = ["Egg", "Eggg"] // ["Egg", "Eggg", "Flour", "Baking Power"]shoppingList[1...3] = ["Egg", "Eggg", "Egggg"] // ["Egg", "Eggg", "Egggg", "Baking Power"]// 通过insert方法在指定下标插入元素//变成:["InsertedValue", "Egg", "Eggg", "Egggg", "Baking Power"]shoppingList.insert("InsertedValue", atIndex: 0) // 通过removeAtIndex移除某个元素// 执行后,变成: ["Egg", "Eggg", "Egggg", "Baking Power"]let removedObj = shoppingList.removeAtIndex(0) // 移除最后一个元素// 方式一:var lastObj = shoppingList.removeLast() // 方式二:var lastObj = shoppingList.removeAtIndex(shoppingList.cout - 1)// 循环迭代数组for item in shoppingList {  println(item)}// 使用了全局函数enumeratefor (index, value) in enumerate(shoppingList) {    println("Item at index \(index + 1) is \(value)")}// 创建和初始化数组var shomInts = Int[]() // 创建空数组,元素的个数为0// 调用初始化器var threeDoubles = Double[](cout: 3, repeatedValue: 0.0)// 通过类型自动推测,不用指定特定类型var anotherThreeDoubles = Array(cout: 3, repeatedValue: 2.4)// 两个数组相加,新数组的类型会根据这两个数组的类型推断出来var sixDoubles = threeDoubles + anotherThreeDoubles/* 字典   字典是存储多个相同类型值的容器。每个值都有一个与之关联的唯一键作为该值在该字典中的唯一标识。   字典中的元素是无序的,与数组不同。当我们需要基于标识来查询值时,我们会使用字典。Swift中的字典   中的键和值的类型必须是明确的,类型为:Dictionary<KeyType, ValueType>,其中KeyType就是键的类型,   而ValueType就是值的类型。对字典的键的唯一限制是这个KeyType必须是可哈希的类型。Swift中的所有基本   数据类型都是可哈希的,这些基本类型都可以作为字典的值。*/// 创建空字典var emptyDict = Dictionary<String, String>()// 初始化var airports: Dictionary<String, String> = ["T": "Tokyo", "D": "Doubin"]// 如果初始化,可以不明确指明类型,可以自动根据初始值推测出来var ariports = ["T": "Tokyo", "D": "Doubin"]// 访问和修改字典元素println("The dictionary of airports contains \(airports.cout) items")// 添加新键值对ariports["L"] = "London"// 通过已经存在的键,修改对应的值airports["L"] = "London Heathrow"// 可以通过updateValue(forKey:)添加或者修改元素if let oldValue = airports.updateValue("Dubin Internation", forKey:"D") {   println("The old value for D was \(oldValue)")}// 通过下标键获取对应的值,返回的是一个Optional类型值,通过optional binding拆包if let airportName = airports["D"] {   println("The name of the airport is \(airportName)")} else {   println("That airport is not in the airports dictionary")}// 通过下标键移除键值对,只需要设置为nilairports["A"] = "Apple International"airport["A"] = nil // 移除// 可以通过removeValueForKey移除if let removeValue = airports.removeValueForKey("D") {   println("The remove airport's name is \(removeValue)")} else {   println("The airports dictionary does not contain a value for D")}// 通过键值对迭代字典for (key, airportName) in airports {  println("\(key): \(airportName)")}for key in airports.keys {  println("Airport code: \(key)")}for airportName in airports.values {   println("Airport name: \(airportName)")}// 把字典所有的值或者键存储到数组中let airportCodes = Array(airports.keys)let airportNames = Array(airport.values) // 创建空字典var namesOfIntegers = Dictionary<Int, String>()namesOfIntegers[10] = "ten" // 有一个键值对namesOfIntegers = [:] // 又变成空字典了,由于前面已经指定过类型了,这里可以重复不指定

0 0
原创粉丝点击