let returnValue = dictionary.updateValue(10, forKey: "number")
let returnValue2 = dictionary.updateValue(30, forKey: "class")
查(遍历字典)
for (key, value) in dictionary {
println("key = \(key), value = \(value)")
}
字典初始化的方法也有两种
使用字典初始化方式创建的是固定键值类型的字典
1.
var initDictionary:[String:String] = [String:String]()
2.
var initDictionary2:Dictionary<String, String> = Dictionary()
控制流
for循环
1.for-in循环
for value in 1...10 {
println("value = \(value)")
}
当我们不需要使用区间的值时,可以使用_告知系统
for _ in 1...10 {
println("good")
}
2.for循环
for var index = 0; index < 10; index++ {
println("index = \(index)")
}
while循环
var whileNumber = 0
while whileNumber < 10 {
println("whileNumber = \(whileNumber)")
whileNumber++
}
do-while循环
do {
println("执行一次")
} while(false)
if-else语句
var aValue = 20
if aValue <= 21 {
println("aValue <= 21")
} else {
println("aValue > 21")
}
switch-case语句
1.在swift中,是没有贯穿的,只会执行满足条件的语句,不用加break也不会贯穿
2.每一个case分支中都必须有一个语句,假如真不想写,可以加break,通过编译
3.swift中,case可以加多个条件
4.若想贯穿可以加fallthrough
var switchNumber = 10
switch switchNumber {
case 10, 3, 2, 8:
println("10")
fallthrough
case 11:
println("11")
case 12:
break
case 13:
println("13")
default :
println("other")
}
5.swift中,switch-case可以进行值的绑定
var (x, y) = (10, 0)
switch (x, y) {
case (let xValue, 0):
println(xValue)
case (10, let yValue):
println(yValue)
default:
println("others")
}
6.swift中,switch-case可以进行区间匹配
var domainNumber = 50
switch domainNumber {
case 0...10:
println("哈哈")
case 11...100:
println("嘿嘿")
case 101...10000:
println("哼哼")
default:
println("others")
}