码迷,mamicode.com
首页 > 编程语言 > 详细

Swift Tour 随笔总结 (4)

时间:2015-01-29 14:02:37      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

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 agood tea sandwich."
case let x where  x.hasSuffix("pepper"):
    let vegetableComment = "Is it a spicy \(x)?"
default:
    let vegetableComment = "Everything tastes good in soup."
}

比起传统的oc有一些新特性,接受了就好挺实用的

不用break

for的拓展

for-in

let interestingNumbers = [
    "Prime": [2, 3, 5, 7, 11, 13],
    "Fibonacci": [1, 1, 2, 3, 5, 8],
    "Square": [1, 4, 9, 16, 25]
]
var largest = 0
for (kind, numbers) in interestingNumbers {
    for number in numbers {
        if number > largest {
            largest = number
        }
    }
}
largest

while/do while保持一致

其他for的拓展用法

var firstForLoop = 0
for i in 0..3{
firstForLoop += i
}
firstForLoop

var secondForLoop = 0
for var i = 0; i < 3; ++i {
    secondForLoop += 1
}
secondForLoop

用过适用 … 代替 .. 则包含upper value(<和<=的差别)

Functions and Closures

func greet(name: String, day: String) -> String {
    returrn "Hellop \(name), today is \(day)."
}
greet("Bob", "Tuesday")

//使用tuple来返回多个值
func getGasPrices() -> (Double, Double, Double) {
    return (3.59, 3.69, 3.79)
}
getGasPrices()

func会使用数组收集多个变量:

func sumOf(numbers: Int...) -> Int {
    var sum = 0
    for number in numbers {
        sum += number
    }
    return sum
}
sumOf()
sumOf(42, 597, 12)

函数的下次接着写吧,挺多的T T

Swift Tour 随笔总结 (4)

标签:

原文地址:http://www.cnblogs.com/zhjch/p/4259369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!