let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a":
println("The letter a")
case "A":
println("The letter A")
default:
println("Not the letter A")
}
let somePoint = (1, 1)
switch somePoint {
case (0, 0): // 位于远点
println("(0, 0) is at the origin")
case (_, 0): // x为任意值,y为0,即在 X 轴上
println("(\(somePoint.0), 0) is on the x-axis")
case (0, _): // y为任意值,x为0,即在 Y 轴上
println("(0, \(somePoint.1)) is on the y-axis")
case (-2...2, -2...2): // 在以原点为中心,边长为4的正方形内。
println("(\(somePoint.0), \(somePoint.1)) is inside the box")
default:
println("(\(somePoint.0), \(somePoint.1)) is outside of the box")
}
// "(1, 1) is inside the box"
let somePoint = (0, 1)
switch somePoint {
case (0, 0):
println("(0, 0) is at the origin")
case (let x, 0):
println("x is \(x)")
case (0, let y):
println("y is \(y)")
default:
println("default")
}
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
println("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
println("(\(x), \(y)) is on the line x == -y")
case let (x, y):
println("(\(x), \(y)) is just some arbitrary point")
}
// "(1, -1) is on the line x == -y”
// for loop (array)
let myArray = [1, 1, 2, 3, 5]
for value in myArray {
if value == 1 {
println("One!")
} else {
println("Not one!")
}
}
// for loop (dictionary)
var dict = [
"name": "Steve Jobs",
"title": "CEO",
"company": "Apple"
]
for (key, value) in dict {
println("\(key): \(value)")
}
// for loop (range)
for i in -1...1 { // [-1, 0, 1]
println(i)
}
// use .. to exclude the last number
// for loop (ignoring the current value of the range on each iteration of the loop)
for _ in 1...3 {
// Do something three times.
}
// while loop
var i = 1
while i < 1000 {
i *= 2
}
// do-while loop
do {
println("hello")
} while 1 == 2
// Switch
let vegetable = "red pepper"
switch vegetable {
case "celery":
let vegetableComment = "Add some raisins and make ants on a log."
case "cucumber", "watercress":
let vegetableComment = "That would make a good tea sandwich."
case let x where x.hasSuffix("pepper"):
let vegetableComment = "Is it a spicy \(x)?"
default: // required (in order to cover all possible input)
let vegetableComment = "Everything tastes good in soup."
}
// Switch to validate plist content
let city:Dictionary<String, AnyObject> = [
"name" : "Qingdao",
"population" : 2_721_000,
"abbr" : "QD"
]
switch (city["name"], city["population"], city["abbr"]) {
case (.Some(let cityName as NSString),
.Some(let pop as NSNumber),
.Some(let abbr as NSString))
where abbr.length == 2:
println("City Name: \(cityName) | Abbr.:\(abbr) Population: \(pop)")
default:
println("Not a valid city")
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有