Swift学习:8.字典
来源:互联网 发布:mysql 查询某一年数据 编辑:程序博客网 时间:2024/06/06 00:12
字典
字典是一种存储多个相同类型的值的容器。每个值(value)都关联唯一的键(key),键作为字典中的这个值数据的标识符。和数组中的数据项不同,字典中的数据项并没有具体顺序。
1.初始化
var games: [String:String] = [“Diablo3”:”2014:8:12”,
“Dragon Age”:”2014:10:07”]
var games = [“Diablo3”:”2014:8:12”,“DragonAge”:”2014:10:07”]
games[“LittleBigPlanet3”] = “2014:11:29"
games[“LittleBigPlanet3”] = “2014:11:30"
var nameOfIntegers = [Int:String]()
namesOfIntegers[16] = “sixteen"
namesOfIntegers = [:]
2.修改已有键值:
if let oldValue = games.updateValue(“2014:8:14”,forKey:”Diablo3”){
println(“Diablo3的旧值:\(oldValue)”)
}
3.获取键值为可选类型:
if let releaseDate = games[“Diablo3”]{
println(“该游戏的发布日期是\(releaseDate)”)
}else {
println(“该游戏的发布日期不在games字典里”)
}
games[“LittleBigPlanet3”] = nil 移除键值
games.removeValueForKey(“Diablo3”) 和updateValue一样
4.字典遍历
let airports = [“TYO”:”Tokyo”,”LHR”:”London”]
for (airportCode, airportName) in airports{
println(“\(airportCode): \(airportName)”)
}
for airportCode in airports.keys{}
for airportName in airports.values{}
5.示例代码:
var airports:Dictionary<String,String> = ["TKO":"Tokyo","CHA":"China"]
println("the airports Dictionary has \(airports.count) airport")
airports["LON"] ="London"
airports["LON"] = "London weather"
airports["CHA"] =nil
iflet oldValue = airports.updateValue("Dublin", forKey:"CHA"){
println("the old value is\(oldValue)")
}else{
println("there is no airport named CHA")
}
for (airportNumber,airportName)in airports{
println("airportNumber:\(airportNumber) airportName:"+airportName)
}
for keyin airports.keys{
}
for valuein airports.values{
}
let airportCode = Array(airports.keys)
var nameOfIntergers = Dictionary<String,Int>()
nameOfIntergers = [:]
- Swift学习:8.字典
- swift 学习<四> 字典
- Swift 基础学习(字典)
- swift Dictionary 字典学习
- Swift学习笔记-字典Dictionary
- Swift学习笔记(6)--字典
- swift简单学习之字典
- swift 3.0 字典简单学习
- Swift 4.0 字典(Dictionary)学习
- Swift学习记录 -- 8.字典的基本操作
- swift学习第五章-字典的使用
- swift学习(三)array 和 字典
- swift学习记录03-集合:数组、字典
- swift学习笔记之数组与字典
- swift 学习笔记 - 数组,字典,元组
- swift学习记录(集合---字典)
- swift基础学习(03)[数组、集合、字典]
- swift学习之路(十)字典
- LinearLayout增加边框
- 解压vmlinuz和解压initrd(initramfs)
- Android Tween and Property Animation的本质区别
- codechef(MAY15)第二题Set Difference
- MySQL与Oracle的分页查询语句
- Swift学习:8.字典
- WampServer安装遇到的问题解决
- 国内开源的框架(学习用)以及语言对应的图书
- SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)
- c++读取obj文件的数据 其中f中只取面片定点数据
- 为什么学到现在任然对计算机浑然不知???门外汉??? 对其运行机制不能理解?
- 致大家
- DOM和AJAX学习
- C语言变长参数实现