Swift--字典的了解
字典存储时,key和value值的类型都是固定的,且都是无序的。
1.字典类型的缩写语法
在swift中,字典的完整格式如下:
Dictionary<Key, Value>
注意:字典的key类型必须符合 哈希算法。
字典的缩写格式如下:
[Key: Value]
虽然完整格式和缩写格式都可以,但是下面介绍字典时主要是以缩写格式为主。
2.创建一个空的字典
当初始化一个空的字典时,可以直接初始化其key和value值的类型,如下:
var namesOfIntegers = [Int: String]()
// namesOfIntegers is an empty [Int: String] dictionary
如下的例子,可以理解为:key为Int类型,value为string类型。
如果字典的元素可以判断出其key和value的值,那么可以创建一个格式为 [:] 的空字典:
namesOfIntegers[16] = "sixteen"
// namesOfIntegers now contains 1 key-value pair
namesOfIntegers = [:]
// namesOfIntegers is once again an empty dictionary of type [Int: String]
3. 创建一个字典并初始化字典的值
在字典中,key和value的值以“:”号区分开,每一对key和value的值用逗号隔开。
[key 1: value 1, key 2: value 2, key 3: value 3]
如下的例子:
var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
正如数组一样,如果创建一个字典并初始化字典的key和value的值时,不需要写字典中key和value值的类型。如下:
var airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
从上面的例子中,swift可以推断出,该字典的key和value的类型都是string类型。
4.字典的取值与修改
你可以用字典的相关方法、属性和下标语法对字典进行取值和修改的操作。
<1>可以用只读属性count来获取字典元素的个数;
print("The airports dictionary contains \(airports.count) items.")
// Prints "The airports dictionary contains 2 items."
<2>用isEmpty的布尔值属性判断字典的count属性是否为0;
if airports.isEmpty {
print("The airports dictionary is empty.")
} else {
print("The airports dictionary is not empty.")
}
// Prints "The airports dictionary is not empty."
<3>用下标语法添加一个新的元素,往字典添加一个新的key和value值,如下:
airports["LHR"] = "London"
// the airports dictionary now contains 3 items
也可以用下标语法来修改字典的值:
airports["LHR"] = "London Heathrow"
// the value for "LHR" has been changed to "London Heathrow"
用updateValue(_:forKey:)的方法来添加或者修改字典key中的value值。如果这个key值存在,就更新这个key的value值;如果这个key值不存在,就添加。与下标不同的是,updateValue(_:forKey:)方法在执行更新后返回key对应的旧值,这使您能够检查是否发生了更新。这个旧值是可选值,如果更新的这个key值存在的话,这个可选值就是更新前key对应value的值,否者的话,返回nil。
if let oldValue = airports.updateValue("Dublin Airport", forKey: "DUB") {
print("The old value for DUB was \(oldValue).")
}
// Prints "The old value for DUB was Dublin."
可以用下标语言来获取一个字典中key对应的value值,因为key可能不存在在字典中,所以该返回值为可选值,如果key值存在,就返回对应的value值;如果不存在,就返回nil;
if let airportName = airports["DUB"] {
print("The name of the airport is \(airportName).")
} else {
print("That airport is not in the airports dictionary.")
}
// Prints "The name of the airport is Dublin Airport."
可以通过把key对应的value值设置为nil的方法来删除key-value的值;
airports["APL"] = "Apple International"
// "Apple International" is not the real airport for APL, so delete it
airports["APL"] = nil
// APL has now been removed from the dictionary
用removeValue(forKey:)的方法删除字典中的key-value值,调用这个方法时,如果key值存在的话,返回的是remove的值,如果不存在就返回nil;
if let removedValue = airports.removeValue(forKey: "DUB") {
print("The removed airport's name is \(removedValue).")
} else {
print("The airports dictionary does not contain a value for DUB.")
}
// Prints "The removed airport's name is Dublin Airport."
5.遍历字典
可以用for-in遍历字典中的key-value值,每一个元素以(key, vlue)的元组返回;您可以将tuple的成员分解为临时常量或变量,作为迭代的一部分;
for (airportCode, airportName) in airports {
print("\(airportCode): \(airportName)")
}
// YYZ: Toronto Pearson
// LHR: London Heathrow
用字典中的keys 和 values 属性来获取字典中所有的属性或者value值;
for airportCode in airports.keys {
print("Airport code: \(airportCode)")
}
// Airport code: YYZ
// Airport code: LHR
for airportName in airports.values {
print("Airport name: \(airportName)")
}
// Airport name: Toronto Pearson
// Airport name: London Heathrow
如果需要将字典中所有的key和value值,以字典的形式返回的话,可以用字典中的keys和values属性来初始化一个新的数组:
let airportCodes = [String](airports.keys)
// airportCodes is ["YYZ", "LHR"] let airportNames = [String](airports.values)
// airportNames is ["Toronto Pearson", "London Heathrow"]
6. 字典是无序的,如果需要对字典排序的话,请对字典的keys 和 values 属性 用sorted() 的方法。
Swift--字典的了解的更多相关文章
- swift字典集合-备
Swift字典表示一种非常复杂的集合,允许按照某个键来访问元素.字典是由两部分集合构成的,一个是键(key)集合,一个是值(value)集合.键集合是不能有重复元素的,而值集合是可以重复的,键和值是成 ...
- Swift 字典 Dictionary基本用法
import UIKit /* 字典的介绍 1.字典允许按照某个键访问元素 2.字典是由两部分组成, 一个键(key)集合, 一个是值(value)集合 3.键集合是不能有重复的元素, 值集合可以有重 ...
- swift - 字典和集合
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #4dbf56 } p.p2 { margin: 0.0px 0. ...
- Swift字典
字典初始化 基本语法: [key 1: value 1, key 2: value 2, key 3: value 3] var airports: Dictionary<String ...
- Swift字典类
在Foundation框架中提供一种字典集合,它是由“键-值”对构成的集合.键集合不能重复,值集合没有特殊要求.键和值集合中的元素可以是任何对象,但是不能是nil.Foundation框架字典类也分为 ...
- Swift字典集合
字典表示一种非常复杂的集合,允许按照某个键来访问元素.字典是由两部分集合构成的,一个是键(key)集合,一个是值(value)集合.键集合是不能有重复元素的,而值集合是可以重复的,键和值是成对出现的. ...
- Swift 字典的经常用法
/* * *要正确使用字典,也须要一些条件 * 1.字典键值对的键和值的类型必须明白,能够直接指定.也能够类似数组直接赋值由编译器自己主动识别 * 2,字典必需要初始化 * 3,键的类型必须是能够被哈 ...
- Swift 字典模型互转总结
现在很多iOS项目的开发开始转向Swift语言. 相信 Swift语言很快会成为iOS工程师 必备技能. 字典转模型, 模型转转字典在开发过程中扮演非常重要的角色. 今天就和大家分享一下使用Swift ...
- iOS开发零基础--Swift教程 字典
字典的介绍 字典允许按照某个键来访问元素 字典是由两部分集合构成的,一个是键(key)集合,一个是值(value)集合 键集合是不能有重复元素的,而值集合是可以重复的,键和值是成对出现的 Swift中 ...
- swift Dictionary 字典
// // main.swift // 字典 // // Created by zhangbiao on 14-6-15. // Copyright (c) 2014年 理想. All rig ...
随机推荐
- LC.exe 已退出,代码为-1 问题解决
最近一个c#工程,之前编译正常.后重装系统,安装DevExpress后,编译一直失败,并提示"4>C:\Windows\Microsoft.NET\Framework\v4.0.303 ...
- (转)使用Spring配置文件实现AOP
http://blog.csdn.net/yerenyuan_pku/article/details/52880558 使用Spring配置文件实现AOP 前面我们已经学会了使用Spring的注解方式 ...
- dockerfile 的最佳实践
Dockerfile 编写nginx容器 [root@mast nginx]# cat Dockerfile FROM centos MAINTAINER zhaoruidong RUN yum -y ...
- Win10 启动64位IE浏览器——修改注册表方法
修改注册表[HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main]下的: "TabProcGrowth"=DWOR ...
- ASP.NetCore 错误 NU1605 检测到包降级: Microsoft.Data.Sqlite 从 2.2.1 降级到 2.1.0
找到使用的.csproj文件 将 <PackageReference Include="Microsoft.Data.Sqlite" Version="2.1.0& ...
- 解决浏览器自动填充input
浏览器输入框自动填充解决办法 emmmmm:今天处理公司后台系统遇到的:登录页面浏览器保存账号密码后:浏览器会自动在其他页面进行填充:解决如下图: 浏览器会默认填充input type值为passwo ...
- 5.1 qbxt 一测 T3
反物质[问题描述] 物理学家有一种假设,世界上存在反物质,反物质遇到正常的物质会发生湮灭. 假设现在有 n 个粒子,每个粒子的种类用一个 m 以内的正整数表示.现在要将这些粒子按一定顺序放入一个封闭空 ...
- 转: 使用 /sys 文件系统访问 Linux 内核
转一个挺不错的文章 使用 /sys 文件系统访问 Linux 内核 https://www.ibm.com/developerworks/cn/linux/l-cn-sysfs/ 如果你正在开发的设备 ...
- 第一讲:vcs simulation basic
要求: 1.complie a verilog/systemverilog design using vcs 2.simulate a verilog/systemverilog design vcs ...
- Counting Kangaroos is Fun 求最少可见袋鼠数
Description There are n kangaroos with pockets. Each kangaroo has a size (integer number). A kangaro ...