//
// ViewController.swift
// SwiftLesson2
//
// Created by 薛雨仑 on 14-10-2.
// Copyright (c) 2014年 Dylan. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
overridefunc viewDidLoad() {
super.viewDidLoad()
/**
循环结构:for、for-in、while、do-while
选择结构:if、switch
注意:这些语句后面一定要跟上大括号{},在C语言中不是必须的
*/
for i in1...10 {
println(i)
}
for i in1..<10 {
println(i)
}
// 如果用不到范围中的值可以使用下划线忽略
for_in1...5 {
println("------")
}
/**
在Swift中,不需要在每一个case后面增加break,执行完case对应的代码后默认会自动退出switch语句
在Swift中,每一个case后面必须有可以执行的语句
case的多条件匹配
1个case后面可以填写多个匹配条件,条件之间用逗号,隔开
case的范围匹配
case后面可以填写一个范围作为匹配条件
*/
let grade = "b"
switch grade {
case"a"..."z":
println("great")
case"b":
println("good")
case"c", "d", "e":
println("soso")
default:
println("----")
}
// case还可以用来匹配元组。比如判断一个点是否在右图的蓝色矩形框内 _ 来忽略值
let point = (1, 1)
switch point {
case (0, 0) :
println("这个点在原点上")
case (_, 0) :
println("这个点在x轴上")
case (0, _) :
println("这个点在y轴上")
case (-2...2, -2...2) :
println("这个点在矩形框内")
default:
println("这个点在其他位置")
}
/**
case的数值绑定
在case匹配的同时,可以将switch中的值绑定给一个特定的常量或者变量,以便在case后面的语句中使用
*/
let point_1 = (10, 20)
switch point_1 {
case (let x, 0): println(x)
case (0, let y): println(y)
default:println("= = = ")
}
/**
where
switch语句可以使用where来增加判断的条件。比如判断一个点是否在右图的绿线或者紫线上
*/
var point_2 = (10, -10)
switch point_2 {
caselet (x2, y2) where x2 == -y2: println("这个点在紫线上")
default: println("这个点不在这2条线上")
}
/**
fallthrough 接着执行
*/
var a = 4
switch a {
case1: println("1")
fallthrough
case2: println("2")
default: println("3")
}
/**
标签明确指定退出哪一个循环
*/
group:
for i in1...3{
for j in1...2 {
println("1")
if j == 2 {
break group
}
}
println("2")
}
}
overridefunc didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}