//{@"id":@"123456",@"name":@"zhangsan"}[]
//定义与初始化
var dic1:[String : String] = ["id":"123456","name":"zhaoyinshuang"]
var dic11:Dictionary<String,String> = ["id":"123456","name":"zhaoyinshuang"]
//如果是Any 必须显示声明字典的类型
letdic2:[String:Any] = ["age":13,"sex":"male"]
let dic22 = Dictionary<String,String>()
//基本使用
dic1.count
dic22.count
dic1.isEmpty
dic22.isEmpty
//添加与修改语法看上去一样
//如果key不存在 就添加 存在就修改
dic1["class"] = "移动互联应用开发171"
dic1["name"] = "lisi"
print(dic1)
dic1.updateValue("wangwu", forKey: "name")
print(dic1)
dic11.removeValue(forKey: "id")
print(dic11)
dic1["name"]
//遍历
var dic:[String : Any] = ["id":"123456789","name":"zhangsan","age":15,"school":"第一中学"]
forkeyindic.keys
{
print(key)
}
forvalueindic.values
{
print(value)
}
print("===============")
for(key, value)indic
{
print("\(key) -- \(value)")
}
print("===============")
for(index, value)indic.enumerated()
{
print("\(index) -- \(value)")
}
var dic3: [String :String] = ["id":"123456","name":"zhaoyinshuang"]
var dic4:[String : String] = ["age":"13","sex":"male","name":"lisi"]
for(key,value)indic4
{
dic3[key] = value
}
print(dic3)
网友评论