ios Swift 备忘录

Variables

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

Constants

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

Strings

var myString = "a"
let myImmutableString = "c"
myString += "b" // ab
myString = myString + myImmutableString // abc
myImmutableString += "d" // compile-time error! let count =
let message = "There are \(count) days in a week"

Logical Operators

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

Printing

let name = "swift"
println("Hello")
println("My name is \(name)")
print("See you ")
print("later")
/* Hello
My name is swift
See you later */

Arrays

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[] // mon
days.insert("tue", atIndex: ) // [mon, tue, thu]
days[] = "wed" // [mon, tue, wed]
days.removeAtIndex() // [tue, wed]

Dictionaries

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

Conditionals

//IF STATEMENT
let happy = true
if happy {
println("We're Happy!")
} else {
println("We're Sad :('")
}
// We're Happy! let speed =
if speed <= {
println("Stationary")
} else if speed <= {
println("Safe speed")
} else {
println("Too fast!")
}
// Safe speed //SWITCH STATEMENT
let n =
switch n {
case :
println("It's 1!")
case ...:
println("It's between 2 and 4!")
case , :
println("It's 5 or 6")
default:
println("Its another number!")
}
// It's between 2 and 4!

For Loops

for var index = ; index < ; ++index {
// loops with index taking values 1,2
}
for index in .. {
// loops with index taking values 1,2
}
for index in ... {
// 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

While Loops

var count =
while count < {
println("count is \(count)")
++count
}
// count is 1
// count is 2 count =
while count < {
println("count is \(count)")
++count
}
// count =
do {
println("count is \(count)")
++count
} while count <
// count is 1
// count is 2 count =
do {
println("count is \(count)")
++count
} while count <
// count is 1

Functions

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

Classes

class Counter {
var count: Int =
func inc() {
count++
}
func add(n: Int) {
count += n
}
func printCount() {
println("Count: \(count)")
}
} var myCount = Counter()
myCount.inc()
myCount.add()
myCount.printCount() // Count: 3

ios Swift 备忘录

上一篇:H5图片预览、压缩、上传


下一篇:@ControllerAdvice详解