Swift备忘录

常量

1
2
let myInt = 1
myInt = 2 // compile-time error!

变量

1
2
3
4
var myInt = 1
var myExplicitInt: Int = 1 // explicit type
var x = 1, y = 2, z = 3 // declare multiple integers
myExplicitInt = 2 // set to another integer value

字符串

1
2
3
4
5
6
7
8
var myString = "a"
let myImmutableString = "c"
myString += "b" // ab
myString = myString + myImmutableString // abc
myImmutableString += "d" // compile-time error!

let count = 7
let message = "There are \(count) days in a week"

字典

1
2
3
4
5
6
7
var days = ["mon": "monday", "tue": "tuseday"]
days["tue"] = "tuesday" // change the value for key "tue"
days["wed"] = "wednesday" // add a new key/value pair

var moreDays: Dictionary = ["thu": "thursday", "fri": "friday"]
moreDays["thu"] = nil // remove thu from the dictionary
moreDays.removeValueForKey("fri") // remove fri from the dictionary

枚举

1
2
3
4
5
enum CollisionType: Int {
case Player = 1
case Enemy = 2
}
var type = CollisionType.Player

数组

1
2
3
4
5
6
7
8
9
10
11
var colors = ["red", "blue"]
var moreColors: String[] = ["orange", "purple"] // explicit type
colors.append("green") // [red, blue, green]
colors += "yellow" // [red, blue, green, yellow]
colors += moreColors // [red, blue, green, yellow, orange, purple]

var days = ["mon", "thu"]
var firstDay = days[0] // mon
days.insert("tue", atIndex: 1) // [mon, tue, thu]
days[2] = "wed" // [mon, tue, wed]
days.removeAtIndex(0) // [tue, wed]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Counter {
var count: Int = 0
func inc() {
count++
}
func add(n: Int) {
count += n
}
func printCount() {
println("Count: \(count)")
}
}

var myCount = Counter()
myCount.inc()
myCount.add(2)
myCount.printCount() // Count: 3

条件判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//IF STATEMENT
let happy = true
if happy {
println("We're Happy!")
} else {
println("We're Sad :('")
}
// We're Happy!

let speed = 28
if speed <= 0 {
println("Stationary")
} else if speed <= 30 {
println("Safe speed")
} else {
println("Too fast!")
}
// Safe speed


//SWITCH STATEMENT
let n = 2
switch n {
case 1:
println("It's 1!")
case 2...4:
println("It's between 2 and 4!")
case 5, 6:
println("It's 5 or 6")
default:
println("Its another number!")
}
// It's between 2 and 4!

循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
for var index = 1; index < 3; ++index {
// loops with index taking values 1,2
}
for index in 1..3 {
// loops with index taking values 1,2
}
for index in 1...3 {
// loops with index taking values 1,2,3
}

let colors = ["red", "blue", "yellow"]
for color in colors {
println("Color: \(color)")
}
// Color: red
// Color: blue
// Color: yellow

let days = ["mon": "monday", "tue": "tuesday"]
for (shortDay, longDay) in days {
println("\(shortDay) is short for \(longDay)")
}
// mon is short for monday
// tue is short for tuesday

函数

1
2
3
4
5
6
7
8
9
func iAdd(a: Int, b: Int) -> Int {
return a + b
}
iAdd(2, 3) // returns 5

func eitherSide(n: Int) -> (nMinusOne: Int, nPlusOne: Int) {
return (n-1, n+1)
}
eitherSide(5) // returns the tuple (4,6)

逻辑操作

1
2
3
4
var happy = true
var sad = !happy // logical NOT, sad = false
var everyoneHappy = happy && sad // logical AND, everyoneHappy = false
var someoneHappy = happy || sad // logical OR, someoneHappy = true

打印(输出)

1
2
3
4
5
6
7
8
9
10
let name = "swift"
println("Hello")
println("My name is \(name)")
print("See you ")
print("later")
/*
Hello
My name is swift
See you later
*/

来源