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
- Swift学习之十一:数组与字典(Array and Dictionary are collection types)
- Swift学习之十一:数组与字典(Array and Dictionary are collection types)
- Swift学习之十一:数组与字典(Array and Dictionary are collection types)
- swift之数组(Array)、集合(Set)、字典(Dictionary)
- 字典与集合(Dictionary与Collection)
- swift学习笔记之数组与字典
- swift Dictionary 字典学习
- swift学习笔记 --- Collection Types
- swift-字典(Dictionary)
- Swift-字典(Dictionary)
- 四 Swift学习之集合类型 (Collection Types)
- Swift学习笔记-字典Dictionary
- Swift 4.0 字典(Dictionary)学习
- excel VBA 字典与集合(Dictionary与Collection)
- VBA 字典与集合(Dictionary与Collection)
- swift 学习计划(一) Array 数组、字典
- Swift学习笔记之基本数据类型-数组与字典
- Swift 集合类型(Collection Type) 之 字典(dictionary)(官方文档翻译及总结)
- C语言函数的可变参数列表
- ISTQB FL初级认证考题分布与学习目标
- 剑指offer面试题30
- ZPL
- 另类手游研发数据 游戏是否受欢迎看会话时长
- Swift学习之十一:数组与字典(Array and Dictionary are collection types)
- 剑指offer面试题31
- 【足迹C++primer】24、顺序容器概述
- 关于JavaScript跨域问题 及 实时刷新
- Swift编程语言入门实战系列教程(五)---增加键盘输入函数
- 剑指offer面试题32
- sqlite操作
- Object literals新特性(非原创)
- hadoop2.2.0源码编译笔记