4

HelloWorld

println("Hello, world")    //木有分号!

变量

var foo = 7
foo = 8

常量

let pi = 3.14

map

var occupations = [
    "Malcolm": "Captain",
    "Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations"

循环判断

for循环和if else

let individualScores = [75, 43, 103, 87, 12]
var teamScore = 0
for score in individualScores {
    if score > 50 {
        teamScore += 3
    } else {
        teamScore += 1
    }
}

while

var n = 2
while n < 100 {
    n = n * 2
}

函数和闭包

函数声明和调用

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

闭包

numbers.map({
    (number: Int) -> Int in
    let result = 3 * number
    return result
    })

class和对象

class Shape {
    var numberOfSides = 0
    func simpleDescription() -> String {
        return "A shape with \(numberOfSides) sides."
    }
}

从class生成对象

var shape = Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()

来自: Apple Inc. “The Swift Programming Language”。 iBooks. https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewBook?id=881256329


Integ
5.6k 声望295 粉丝

全栈溢出工程师,兼职拆段错误水表,代写内核紧张拉丁语情书。


« 上一篇
Yeoman-Bower-Grunt
下一篇 »
杭JS收获总结