swift * 字典/Dictionary初始化以及增、删、改、遍历
来源:互联网 发布:淘宝购女童装 编辑:程序博客网 时间:2024/05/22 06:42
学习笔记
1、字典初始化
vardict = [:] //初始化无类型空字典
dict = ["1":"aaa","2":"bbb"]
print(dict)
dict = [1:"1","2":2]//key 和value都是不定类型的
print(dict)
let dict2:Dictionary<String,Int> = ["1":111,"2":222] //限定键值类型
print(dict2)
letdict3:[String:Int] = ["1":111]
print(dict3)
2、字典操作
vardict4 = ["apple":"苹果","bag":"包"]
print(dict4["apple"])
dict4["apple"] ="橘子"//修改值
print(dict4)
dict4.updateValue("苹果", forKey: "apple")//更新键值如果key存在更新键值对并且返回key之前的对应的value
print(dict4)
print(dict4.updateValue("苹果", forKey: "apples")) //如果key不存在将增加新的键值对并返回nil
iflet origin = dict4.updateValue("aaaaa", forKey: "appl") {
print("origin\(origin)")
}
print(dict4)
dict4["an"] == "一个" //增加/修改键值对
iflet origin = dict4.removeValueForKey("apple") {
print("删除key\(origin)")//如果key存在删除对应的键值对 并返回key对应的值
print(dict4)
}else {
//不存在key返回nil字典不变
}
if let origin = dict4.removeValueForKey("aaaaa") {
print(origin)
}else {
print(dict4.removeValueForKey("aaaaa"))//如果key不存在不作操作并返回nil
}
print(dict4)
3、字典遍历
for(key,value) in dict4 { //使用元组 遍历键值对
print("\(key) : \(value)")
}
forkey indict4.keys{ //遍历所有key
print(key)
}
forvalue indict4.values{ //遍历所有value
print(value)
}
0 0
- swift * 字典/Dictionary初始化以及增、删、改、遍历
- swift*数组Array的初始化以及增、删、改、遍历
- Swift 系统学习 10 字典 Dictionary 增删改查
- swift Dictionary 字典
- Swift 字典Dictionary
- swift中的字典Dictionary
- Swift 字典Dictionary
- Swift 字典(Dictionary)
- swift-字典(Dictionary)
- iOS - Swift Dictionary 字典
- Swift-字典(Dictionary)
- swift Dictionary 字典学习
- Swift 中的字典 (Dictionary)
- C# 字典 Dictionary 遍历
- C# 字典 Dictionary 遍历
- 3.10 Swift字典的操作 增 删 改 查
- Dictionary.字典,增删改查
- Swift学习笔记-字典Dictionary
- chat function realizing based on epoll
- 建立 Annotation版本的hibernate小例子
- iOS开发之-------监听textview编辑状态
- Sql 查看未提交事务 未提交事务
- ubuntu下简单使用activemq
- swift * 字典/Dictionary初始化以及增、删、改、遍历
- 30个你 “ 不可能全部会做 ” 的javascript题目答案
- 图片自适应页面等比缩放
- Failed to connect to the configuration database(Sharepoint 2013配置向导失败)
- 输出两个数中的最大值(函数)
- 技巧
- JasperReport学习笔记6-JRXML的标签
- 数据库的事物机制
- delphi2010 域名转换IP