1.创建与初始化字典
var student:Dictionary<String,Int> = ["C":10001,"C++":10002,"C#":10003]
2.向字典添加键值对: KEY->LUA,VALUE->10004
student["LUA"]=10004
3.普通遍历k为键,v为值
for (k,v) in student //无序
{
print("key:\(k) value:\(v)")
}
4.元组方式遍历
//遍历字典(元组模式)
for tuples in student //无序
{
print("key0 of tuples :\(tuples.0) key1 of tuples:\(tuples.1)")
}
5.只遍历键
//通过键遍历
for k in student.keys
{
print("key=:\(k)")
}
6.只遍历值
//通过值遍历
for v in student.values
{
print("value=:\(v)")
}
演示效果: