Open JingchaoZhao opened 5 years ago
implicit explicit
let implictDouble = 70.0 let explicitDouble: Double = 70
values are never implicitly converted to another type. you need to convert a value to a different type
let label = "test" let width = 70 let wLable = label + String(width)
let a = 3 let b = 4 let aStr = "hahaha (a)" let allStr = "hahahah (a + b)" use () to include a floating-point calculation in a string
three double quotation marks (""") for strings that take up multiple lines """ asdfas asgssgf """
array and dictionaries with []
control flow
? after the type of a value to mark the value as optional.
use if and let together to work with values that might be missing. These values are represented as optionals.
switch
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 } } } print(largest)
var n = 2 while n < 100 { n *= 2 } print(n) // Prints "128"
var m = 2 repeat { m *= 2 } while m < 100 print(m) // Prints "128"
var total = 0 for i in 0..<4 { total += i } print(total) // Prints "6"
use ... to make a range that includes both values. using ..< to make a range of indexes. ... ..<
control flow
? after the type of a value to mark the value as optional.
use if and let together to work with values that might be missing. These values are represented as optionals.
switch
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 } } } print(largest)
var n = 2 while n < 100 { n *= 2 } print(n) // Prints "128"
var m = 2 repeat { m *= 2 } while m < 100 print(m) // Prints "128"
var total = 0 for i in 0..<4 { total += i } print(total) // Prints "6"
use ... to make a range that includes both values. using ..< to make a range of indexes.
Functions and Closures
func greet(person: String, day: String) -> String { return "Hello (person), today is (day)." } greet(person: "Bob", day: "Tuesday")
write a custom argument label before the parameter name, or white to use no argument label func greet( person: String, on day: String) -> String { return "Hello (person), today is (day)." } greet("John", on: "Wednesday")
to return multiple values from a function;
func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int) { var min = scores[0] var max = scores[0] var sum = 0
for score in scores {
if score > max {
max = score
} else if score < min {
min = score
}
sum += score
}
return (min, max, sum)
} let statistics = calculateStatistics(scores: [5, 3, 100, 3, 9]) print(statistics.sum) // Prints "120" print(statistics.2) // Prints "120"
nested functions. for organise the code in a function that is long or complex. func returnFifteen() -> Int { var y = 10 func add() { y += 5 } add() return y } returnFifteen()
functions are a first-class type. a function can return anther functions as its value
func makeIncrementer() -> ((Int) -> Int) { func addOne(number: Int) -> Int { return 1 + number } return addOne } var increment = makeIncrementer() increment(7)
a function can take another functions as one of its arguments'
func hasAnyMatches(list: [Int], condition: (Int) -> Bool) -> Bool { for item in list { if condition(item) { return true } } return false } func lessThanTen(number: Int) -> Bool { return number < 10 } var numbers = [20, 19, 7, 12] hasAnyMatches(list: numbers, condition: lessThanTen)
functions are actually a special case of closures: blocks of code that can be called later.
numbers.map({ (number: Int) -> Int in let result = 3 * number return result })
let mappedNumbers = numbers.map({ number in 3 * number }) print(mappedNumbers) // Prints "[60, 57, 21, 36]"
let sortedNumbers = numbers.sorted { $0 > $1 } print(sortedNumbers) // Prints "[20, 19, 12, 7]"
var make a variable ket make a constant
var myVarible = 42 myVariable = 50 let myConstant = 42