美文网首页
Swift学习之十一:数组与字典(Array and Dicti

Swift学习之十一:数组与字典(Array and Dicti

作者: 一粒吗 | 来源:发表于2015-10-27 20:47 被阅读82次

/* Swift 提供了两种集合类型,即数组(Array)和字典(Dictionary),存储值的集合

数组存储相同类型的顺序列表值。字典存储无序同类型值的集合,通过键来查询和

引用。

在Swift中,数组和字典总是值和键的存储类型是明确的。这意味着不能插入错误的类型到字典

和数组中。这种显示类型可以保证你的代码中值的类型总是明确的。

*/

// 数组类型全型为Array,也可以使用SomeType[]这种写法。虽然这两种类型是一样的,但

// 后者更佳,并且都会使用后者。

varshoppingList: String[] = ["Egg","Milk"]

// 访问和修改数组元素

println("The shopping list contains \(shoppingList.cout) items")

// 判断数组是否是空数组

ifshoppingList.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)

// 循环迭代数组

foritem in shoppingList {

println(item)

}

// 使用了全局函数enumerate

for(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必须是可哈希的类型。Swift中的所有基本

数据类型都是可哈希的,这些基本类型都可以作为字典的值。

*/

// 创建空字典

var emptyDict = Dictionary()

// 初始化

varairports: Dictionary = ["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:)添加或者修改元素

iflet oldValue = airports.updateValue("Dubin Internation",forKey:"D") {

println("The old value for D was \(oldValue)")

}

// 通过下标键获取对应的值,返回的是一个Optional类型值,通过optional binding拆包

iflet airportName = airports["D"] {

println("The name of the airport is \(airportName)")

}else{

println("That airport is not in the airports dictionary")

}

// 通过下标键移除键值对,只需要设置为nil

airports["A"] ="Apple International"

airport["A"] =nil// 移除

// 可以通过removeValueForKey移除

iflet 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)")

}

forkey in airports.keys{

println("Airport code: \(key)")

}

forairportName in airports.values{

println("Airport name: \(airportName)")

}

// 把字典所有的值或者键存储到数组中

let airportCodes = Array(airports.keys)

let airportNames = Array(airport.values)

// 创建空字典

var namesOfIntegers = Dictionary()

namesOfIntegers[10] ="ten"// 有一个键值对

namesOfIntegers = [:]// 又变成空字典了,由于前面已经指定过类型了,这里可以重复不指定

相关文章

  • Swift学习之十一:数组与字典(Array and Dicti

    /* Swift 提供了两种集合类型,即数组(Array)和字典(Dictionary),存储值的集合 数组存储相...

  • swift 集合类型

    标签(空格分隔): swift array dictionary zybuluo Swift 语言里的数组和字典中...

  • Note 3 Swift (1)

    Swift学习笔记 变量与常量 变量 常量 值类型与引用类型 值类型 引用类型 数组与字典 数组 字典

  • 1.4 集合类型

    Swift中集合类型: 数组 array 字典 dictionary 一.数组 官方解释:数组使用有序列表储存相同...

  • Swift集合类型扩展

    对于Swift来说, 数组(Array)、字典(Dictoinary)、集合(Set) 统称集合类型(Collec...

  • 4 Collection Types 集合类型

    Swift提供了三种主要的集合类型,array数组, set集合, dictionary字典,用于存储值集合。数组...

  • 4、【Swift】集合类型

    数组(Array):有序 集合(Set):无序无重复 字典(Dictionary):无序的键值对 Swift 的数...

  • 4、集合类型

    Swift提供集合类型: 数组(Array):有序数据; 集合(Set):无序、无重复数据; 字典(Diction...

  • Swift 3.0之四、集合类型

    Swift 提供了三种主要的集合类型:数组Array、合集Set还有字典Dictionary。数组是有序的值的集合...

  • Swift5.1集合类型

    4.集合类型 集合类型:Swift 语⾔提供数组(Array)、集合(Set)和字典(Dictionary)三种基...

网友评论

      本文标题:Swift学习之十一:数组与字典(Array and Dicti

      本文链接:https://www.haomeiwen.com/subject/nybxhttx.html