1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| //: [Previous](@previous)
import Foundation
//: ## 数组 // 组建一个空数组 var someInts = [Int]() // 通过赋值,someInts被推断为[Int]类型的数组 someInts.count // output: 0
// 创建一个带有默认值的数组 var threeDoubles = [Double](count: 3, repeatedValue: 0.0)
// 相种存在数据的相同类型数据可进行相加 // 可用字面量构造数组,类型相同,数组类型会被自动推导出来 var twoDoubles = [2.4, 2.5] var fiveDoubles = threeDoubles + twoDoubles print(fiveDoubles.count) // output: 5
// 可使用布尔值属性 isEmpty 作数据数量作检查 someInts.isEmpty fiveDoubles.isEmpty
// 使用append(_:)方法在数组后面添加新的同类型数据项 someInts.append(5) print(someInts) // output: [5]
// 使用下标来获取数组中的数据项 var firstItem = fiveDoubles[0]
// 可使用下查来修改数据项的值 fiveDoubles[0] = 10 print(fiveDoubles) // output: [10, 0, 0, 2.4, 2.5]
// 使用insert(_:atIndex:)在某个具体索引位置之前添加数据 fiveDoubles.insert(20.0, atIndex: 0) print(fiveDoubles) // output: [20.0, 10.0, 0.0, 0.0, 2.4, 2.5]
// 使用removeAtIndex(_:)除索引项数据 fiveDoubles.removeAtIndex(0) print(fiveDoubles.count) // output: 5
// 数组遍历 使用用语法 for-in 来进行 for i in fiveDoubles { print("Item: \(i)") } // //Item: 10.0 // //Item: 0.0 // //Item: 0.0 // //Item: 2.4 // //Item: 2.5
//: ## 集合 // 创建一个Character类型的集合 var letters = Set<Character>() letters.insert("B")
// var stringSet: Set = ["aaaa", "aaaa", "bbbb"] print("集合中的数据条数:\(stringSet.count), 分别是:\(stringSet)") // 集合中的数据条数:2, 分别是:["bbbb", "aaaa"] // 集合数据没有顺序之分,但却不能重复。 // 在数据输出这时却是可以作一个输出上的排序 for str in stringSet.sort() { print("item: \(str)") } //item: aaaa // //item: bbbb
//: ## 字典 var nameOfItegers = [Int: String]() // nameOfItegers 是一个空的 [Int: String] 字典 nameOfItegers[16] = "sixteen" // 添加值 nameOfItegers = [:] // 清空
// 和数组一样,也可以根据字面量来构造字典 let student = ["张三": "001", "李四": "002"] // 类型:[String : String] // 取值可使用索引键即可 let zhansan = student["张三"] // 如果键值不存在,则直接返回nil if let name = student["Andy"] { print("name: \(name)") } else { print("名称不在字典里") } //名称不在字典里 // 字典的遍历可使用 for-in ,值是(key, value) 的形式 for (name, number) in student { print("Name: \(name), Number: \(number)") } //Name: 张三, Number: 001 //Name: 李四, Number: 002
|