swift语言学习笔记(4)
2014-12-13 09:27
246 查看
控制流
For-In
循环来遍历一个集合里面的所有元素,例如数字表示的范围、数组中元素和字符串中的字符
for condition increment
注意:condition是每次循环的开始时自动赋值的常量,隐式声明,无需自己声明let。
condition常量只存在于循环的生命周期里。想condition在循环之后的值使用,可以循环之前声明为一个变量;同时,如果不需要范围中每一项,可以用下划线_来代替(condition)来忽略.
//遍历范围
for index in 1...5 {
println(index)
}
//1
//2
//3
//4
//5
//忽略每一项
for _ in 1..4 {
println("fine")
}
//显式声明index (存疑?)
var index = 0,result = 0
for index in 1...3 {
result += index
}
println("\(index) \(result)") //3 6
//遍历字符串
for charResult in "something" {
println(charResult)
}
//遍历数组
let names = ["Anna","Alex","Jack"]
for name in names {
println(name)
}
//遍历字典
let colorNum = ["red":1,"blue":2,"yellow":3]
for (color,num) in colorNum {
println("this color \(color) num is \(num)")
}
switch
不用写break,执行完case语句自动结束switch语句
如果想贯穿case,可以显式case语句中声明fallthrough
let someNum = 0
switch someNum {
case 1,2,3:
//to do
case 4,5 :
//to do
fallthrough
default :
//to do
}
//范围匹配
let count = 1000
swith count {
case 0 :
println("this is nothing")
case 1...100 :
println("hundreds")
case 101...1000 :
println("thousands")
default:
println("this is over")
}
//匹配元组
1、匹配元组中值、范围和下划线(所有值)
let somePoint = (1,1)
switch somePoint {
case (0,0) :
//to do
case (_,0):
//to do
case (-1...1,1...1) :
//to do
default :
//to do
}
//值绑定
let anotherPoint = (2,0)
switch anotherPoint {
case (let x,0) :
println("on the x is \(x)")
case (0,let y) :
println("on the y is \(y)")
case let (x ,y)
//to do
}
where
let yetAnotherPoint = (1,-1)
switch yetAnotherPoint {
case let (x,y) where x == y :
println("x == y")
case let (x,y) where x == -y :
println("x == -y")
case let (x,y) :
//to do
}
For-In
循环来遍历一个集合里面的所有元素,例如数字表示的范围、数组中元素和字符串中的字符
for condition increment
注意:condition是每次循环的开始时自动赋值的常量,隐式声明,无需自己声明let。
condition常量只存在于循环的生命周期里。想condition在循环之后的值使用,可以循环之前声明为一个变量;同时,如果不需要范围中每一项,可以用下划线_来代替(condition)来忽略.
//遍历范围
for index in 1...5 {
println(index)
}
//1
//2
//3
//4
//5
//忽略每一项
for _ in 1..4 {
println("fine")
}
//显式声明index (存疑?)
var index = 0,result = 0
for index in 1...3 {
result += index
}
println("\(index) \(result)") //3 6
//遍历字符串
for charResult in "something" {
println(charResult)
}
//遍历数组
let names = ["Anna","Alex","Jack"]
for name in names {
println(name)
}
//遍历字典
let colorNum = ["red":1,"blue":2,"yellow":3]
for (color,num) in colorNum {
println("this color \(color) num is \(num)")
}
switch
不用写break,执行完case语句自动结束switch语句
如果想贯穿case,可以显式case语句中声明fallthrough
let someNum = 0
switch someNum {
case 1,2,3:
//to do
case 4,5 :
//to do
fallthrough
default :
//to do
}
//范围匹配
let count = 1000
swith count {
case 0 :
println("this is nothing")
case 1...100 :
println("hundreds")
case 101...1000 :
println("thousands")
default:
println("this is over")
}
//匹配元组
1、匹配元组中值、范围和下划线(所有值)
let somePoint = (1,1)
switch somePoint {
case (0,0) :
//to do
case (_,0):
//to do
case (-1...1,1...1) :
//to do
default :
//to do
}
//值绑定
let anotherPoint = (2,0)
switch anotherPoint {
case (let x,0) :
println("on the x is \(x)")
case (0,let y) :
println("on the y is \(y)")
case let (x ,y)
//to do
}
where
let yetAnotherPoint = (1,-1)
switch yetAnotherPoint {
case let (x,y) where x == y :
println("x == y")
case let (x,y) where x == -y :
println("x == -y")
case let (x,y) :
//to do
}
相关文章推荐
- swift语言的学习笔记九(OC与Swift混编)
- Swift语言学习笔记(4)
- swift语言的学习笔记十(block)
- swift语言的学习笔记十一(willSet与didSet)
- Swift语言学习笔记(七)
- swift语言的学习笔记五(线程)
- swift语言学习笔记(1)
- Swift学习笔记一:语言特性
- swift语言的学习笔记十二(willSet与didSet)
- Swift语言学习笔记(六)
- swift语言的学习笔记九(OC与Swift混编)
- swift语言的学习笔记四-2(对上一节有些遗留进行处理)
- swift语言的学习笔记十一(performSelector)
- swift语言的学习笔记五(线程)
- ios学习笔记图片+图片解释(c语言 oc语言 ios控件 ios小项目 ios小功能 swift都有而且笔记完整喔)
- swift语言_学习笔记
- Java2核心技术第七版的学习笔记(三) Fundamental Programming Structures in Java(Java语言的基础)(四):
- C++程序设计语言特别版 学习笔记(0)
- Java2核心技术第七版的学习笔记(三) Fundamental Programming Structures in Java(Java语言的基础)(三)
- 关于LINQ(语言集成查询)的一些学习笔记(没实践过的)