您的位置:首页 > 移动开发 > Swift

Swift4.0 Dictionary使用详解

2020-07-15 06:10 731 查看

  字典是一种存储相同类型多重数据的存储器。每个值(value)都关联独特的键(key),键作为字典中的这个值数据的标识符。和数组中的数据项不同,字典中的数据项并没有具体顺序。我们在需要通过标识符(键)访问数据的时候使用字典。

字典必须满足的条件:

 (1)字典键值对的键和值的类型必须明确,可以直接指定,也可以类似数组直接赋值由编译器自动识别(在自动识别时字典里的类型必须是同一种类型)

 (2)字典必须要初始化

 (3)键的类型基本数据类型和遵循哈希(Hashable)协议的类

 

1、初始化一个空字典

//        //初始化一个空字典
var dict1:Dictionary<Int,String> = [:]//其中键是Int型  值是String型
var dict2:[String:String] = [:]//其中键、值都是String型
var dict3 = [String:String]()
var dict4 = Dictionary<String,String>()

 

2、字典的增、删、改、查

//        //增、删、改、查
//其中as后的[String : Any]是字典的 键、值类型
var dict = ["name":"sunfusheng", "age":20, "blog":"sunfusheng.com"] as [String : Any]
//       1、增、改
//        有则更新键值对的值,无则增加一个键值对
dict["height"] = 60
print("dict = \(dict) one = \(String(describing: dict["age"]))")

/**
打印结果为:dict = ["name": "sunfusheng", "age": 20, "height": 60, "blog": "sunfusheng.com"] one = Optional(20)
*/

// updateValue: 有则更新键值对的值,无则增加一个键值对,如果字典里含有updateValue的Key那就返回老大值,否则返回nil(nil在Swift中,nil不是指针,它是一个确定的值,用于表示值缺失。任何类型的可选状态都可以设置为nil,不只是对象类型)
dict.updateValue("sina", forKey: "weiBo")
let backValue = dict.updateValue(28, forKey: "age")

//       2、 查
/**
这里的:!强制对Optional值进行拆包
在Swift中,!和 ?都是拆包但他两的使用场景不一样
!的使用场景:
1)强制对Optional值进行拆包
2)声明隐式拆包变量,一般用于类中的属性

?的使用场景:
1)声明Optional值变量
2)在对Optional值操作中,用来判断是否能响应后面的操作
*/
let age:Any = dict["age"] as! Int

print("dict = \(dict) \n backValue = \(String(describing: backValue)) \n age = \(age)")

/**
打印结果为:
dict = ["name": "sunfusheng", "age": 28, "height": 60, "weiBo": "sina", "blog": "sunfusheng.com"]
backValue = Optional(20)
age = 28

*/

//        3、删

//        (1)可以使用下标语法来通过给某个键的对应值 赋值 为 “nil” 来从字典里 移除一个键值对
dict["height"] = nil
print("nowDic = \(dict)")
/**
打印结果为:
nowDic = ["name": "sunfusheng", "age": 28, "weiBo": "sina", "blog": "sunfusheng.com"]
字典里的 "height": 60 已经被移除
*/

/**
(2)通过removeValue(forKey:)这个方法也可以移除键值对,这个方法在键值对存在的情况下会移除该键值对并返回被移除的值(在没有值的情况下返回nil)
*/
let removeValure = dict.removeValue(forKey: "name")
print("dict = \(dict) \n removeValure = \(String(describing: removeValure))")
/**
打印结果为:
dict = ["age": 28, "weiBo": "sina", "blog": "sunfusheng.com"]
removeValure = Optional("sunfusheng")

有结果可以得:字典里的 "name": "sunfusheng" 已经被移除
*/

//        (3)removeAll() 清空字典的所有键值对
dict.removeAll()

 

3、判断字典是否为空

let dict1:Dictionary<Int,String> = [:]//其中键是Int型  值是String型
let dict = ["name":"sunfusheng", "age":20, "blog":"sunfusheng.com"] as [String : Any]

//        1、通过isEmpty判断
if dict1.isEmpty {
print("dict1 字典为空")
} else {
print("dict1 不为空 = \(dict1)")
}

if dict.isEmpty {
print("dict 字典为空")
} else {
print("dict 不为空 = \(dict)")
}
/**
打印结果:
dict1 字典为空
dict 不为空 = ["name": "sunfusheng", "age": 20, "blog": "sunfusheng.com"]
*/

//        2、通过字典的key的数组来判断
let keys1 = dict1.keys.count
let keys = dict.keys.count
if keys1 == 0 {
print("dict1 字典为空")
} else {
print("dict1 不为空 = \(dict1)")
}

if keys == 0 {
print("dict 字典为空")
} else {
print("dict 不为空 = \(dict)")
}
/**
打印结果:
dict1 字典为空
dict 不为空 = ["name": "sunfusheng", "age": 20, "blog": "sunfusheng.com"]

两次结果一样
*/

 

4、遍历字典

let dict = ["name":"sunfusheng", "age":20, "blog":"sunfusheng.com"] as [String : Any]
//字典的遍历,使用for - in 循环来遍历字典中的键值对,每个字典中的数据项都以(key,value)元组的形式返回,可以使用临时常量或者变量来分解这些元组
for dictValure in dict {
print("字典遍历的结果:\(dictValure)")
}
/**
输出结果:
字典遍历的结果:(key: "name", value: "sunfusheng")
字典遍历的结果:(key: "age", value: 20)
字典遍历的结果:(key: "blog", value: "sunfusheng.com")
*/

//可以单独遍历出字典里的所有keys 或 values值
//        (1)获取字典中所有的键
for dictKey in dict.keys {
print("dictKey = \(dictKey)")
}
/**
输出结果:
dictKey = name
dictKey = age
dictKey = blog
*/
//        (2)获取字典中的所有值
for dictValue in dict.values {
print("dictValue = \(dictValue)")
}
/**
输出结果:
dictValue = sunfusheng
dictValue = 20
dictValue = sunfusheng.com
*/

//当只是需要使用某个字典的键集合或者值集合来作为某个接收Array 可以直接使用keys或者values属性构造一个新数组
let dictKeyArray = [String](dict.keys)
let dictValuesArray = [Any](dict.values)
print("dictKeyArray = \(dictKeyArray) \n dictValuesArray = \(dictValuesArray)")
/**
输出结果:
dictKeyArray = ["name", "age", "blog"]
dictValuesArray = ["sunfusheng", 20, "sunfusheng.com"]
*/

 

转载于:https://www.cnblogs.com/liYongJun0526/p/7543173.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: