淺入淺出Swift

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 elseswift

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
}

函數和閉包

函數聲明和調用app

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

閉包ide

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=881256329code

相關文章
相關標籤/搜索