Dictionarys
var dictionary = [1:"A",2:"B",3:"C"]
- 字典存储的数据是键和值的数据对
- 键和值可以是任意数据类型
- 一个字典只能存储一种键和值的数据类型搭配
字典的意义在于可以通过键直接访问值,速度快。字典是无序的。
Dictionary Type
- Dictionary<Int , String>
-
Dictionary<String , String>
...... - [Int:String]
-
[String:String]
......
字典的声明
//字典的声明
var course = [1:"Java Programing",2:"C++",3:"Swift"]
var website = ["Search":"Baidu","Learn":"iMooc","News":"NetEase"]
var course1 : Dictionary<Int,String> = [1:"Java Programing",2:"C++",3:"Swift"]
var website1 : [String:String] = ["Search":"Baidu","Learn":"iMooc","News":"NetEase"]
//空字典的声明
var empty = Dictionary<Int,String>()
var empty1 = [String:String]()
当访问字典中得数据时,返回的是optional(可选型)数据,例如:
var strStr = "I want to learn" + course1[3]!//这样写才能拼接字符串
for-in循环
for (key,value) in course1{
println("\(key):\(value)")
}
网友评论