for, while, if, guard, switch, break, continue
λ€λ₯Έ μΈμ΄μ κ±°μ λμΌνλ€κ³ λ΄λ 무방νλ€.
λ°°μ΄, μ«μ λ²μ, λ¬Έμμ΄, λμ λ리, ... μ for - in 루νλ₯Ό μ μ©ν΄μ μμ νλνλμ μ κ·Όν μ μλ€.
μ λ΄μ©μμ λμ²΄λ‘ λ€λ€μκ³ , μ΅μνκΈ° λλ¬Έμ λ΄κ° λ무λλ μ μμ΄λ²λ¦¬λ
for (index, value) in enumarated(arr) {
}
for i in stride(from: 0, to: 10, by: 2) {
// 10 μ ν¬ν¨ μλ¨.
}
for i in stride(from: 0, through: 10, by: 2) {
// 10 ν¬ν¨.
}
λ§ μ°κ³ λμ΄κ°λλ‘ νκ² λ€. νμ΄μ¬μμλ enumerate(arr) / range(0, 10, 2) μ΄λΌ νμ ν·κ°λ¦Ό
λ€λ₯Έ μΈμ΄μ while, do ~ while κ³Ό κ°λ€. μ‘°κ±΄μ΄ true μΈ λμ λ°λ³΅ / 첫λ²μ§Έλ 쑰건 λ°μ§μ§ μκ³ μ€ν ν λ°λ³΅.
Swift switch μ νΉμ§
switch int(score / 10) {
case 10, 9: print("A") // , λ‘ λλκΈ°λ κ°λ₯.
case 8: print("B")
case 7: print("C")
case 6: print("D")
default: print("F")
}
// μ«μ λ²μλ‘ μ°λ κ²λ κ°λ₯νλ€.
switch score {
case 90...100: print("A")
case 80..<90: print("B")
case 70..<80: print("C")
case 60..<70: print("D")
default: print("F")
}
case λ₯Ό 무μνλ €λ©΄ _ λ‘ λ£μ΄μ£Όλ©΄ λλ€.
let somePoint = (1, 1)
switch somePoint {
case (0, 0):
print("\(somePoint) is at the origin")
case (_, 0):
print("\(somePoint) is on the x-axis")
case (0, _):
print("\(somePoint) \(y) is on the y-axis")
case (-2...2, -2...2):
print("\(somePoint) is inside the box")
default:
print("\(somePoint) is outside of the box")
}
// Prints "(1, 1) is inside the box"
κ°μ λ°μΈλ©ν΄μ μ¬μ©ν μλ μκ³ , where μ μ μ΄μ©ν΄μ λ°μΈλ©ν κ°μ 쑰건μ λ°μ§ μλ μλ€.
let anotherPoint = (2, 0)
switch anotherPoint {
case (let x, 0):
print("on the x-axis with an x value of \(x)")
case (0, let y):
print("on the y-axis with a y value of \(y)")
case let (x, y) where x == y:
print("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
print("(\(x), \(y)) is on the line x == -y")
case let (x, y):
print("(\(x), \(y)) is just some arbitrary point")
}
// Prints "on the x-axis with an x value of 2"
continue, break, fallthrough, return(ν¨μ), throw(μλ¬ νΈλ€λ§)
νμ¬ λ°λ³΅νκ³ μλ λ¨κ³λ₯Ό 건λλ°κ³ , λ€μ λ°λ³΅μ μμ.
νμ¬ λ°λ³΅λ¬Έμ λΉ μ Έλκ°.
or switch λ¬Έμμ break μμ μλ 쑰건μ μ μΈν κ²μ 무μ. default: break
switch λ¬Έμ΄ C μ²λΌ λμν μ μκ², λ€μ λ¨κ³λ‘ νλ₯΄κ² λ§λ€μ΄μ€λ€.
+) λΌλ²¨μ μ΄μ©ν΄μ νΉμ λ°λ³΅λ¬Έμ λΉ μ Έλκ° μ μμ§λ§, μ μ μ°μ.
gameLoop: while square != finalSquare {
diceRoll += 1
if diceRoll == 7 { diceRoll = 1 }
switch square + diceRoll {
case finalSquare:
// diceRoll will move us to the final square, so the game is over
break gameLoop
case let newSquare where newSquare > finalSquare:
// diceRoll will move us beyond the final square, so roll again
continue gameLoop
default:
// this is a valid move, so find out its effect
square += diceRoll
square += board[square]
}
}
print("Game over!")
ν¨μλ ν΄λ‘μ λ΄μμ, μ΄λ€ 쑰건μ λ§μ‘±ν΄μΌνλ λ³μμ λ°λΌ λ€μ λ¬Έμ₯μ μ€νμ κ²°μ νλ€.
π ν΄λΉ λ³μλ₯Ό ν λΉνλ λ¬Έμ₯μ κ²μ¬ν΄μ true μ΄λ©΄ λ€μ λ¬Έμ₯μ μ€ννκ³ , κ·Έ μ‘°κ±΄μ΄ κ±°μ§μ΄ λλ©΄ μμ λΈλ‘μ νμΆνλ λ¬Έμ₯μ else λ¬Έμ μμ±νλ€.
π 보ν΅μ else λ¬Έμμ return μ΄ μ¬μ©λμ§λ§, μν©μ λ°λΌ break continue throw λͺ¨λ μ¬μ© κ°λ₯. νμΆμ΄λκΉ~
π μμΈ μν©λ§μ μ²λ¦¬νκ³ μΆμ λ if λμ guard λ₯Ό μ¬μ©νλ€.
func greet(person: [String: String]) {
// λμ
λ리μ name μ΄ μμΌλ©΄ κ·Έλ₯ λλ¨.
guard let name = person["name"] else { return }
print("Hello \(name)!")
// λμ
λ리μ μμΉ μ λ³΄κ° μμΌλ©΄ μ΄λ¦, μλ΄λ¬Έκ΅¬λ§ λ½κ³ λλ¨.
// μ΅μ
λ λ°μΈλ©λ κ°λ₯.
guard let location = person["location"] else {
print("I hope the weather is nice near you.")
return
}
print("I hope the weather is nice in \(location).")
}
// OS λ²μ Όμμ κ°λ₯ν API μΈμ§ 체ν¬ν΄μ μ¬μ©νλλ° μ¬μ©νλ€.
// guard λ¬ΈμΌλ‘λ μ¬μ© κ°λ₯
if #available(iOS 10, macOS 10.12, *) {
// Use iOS 10 APIs on iOS, and use macOS 10.12 APIs on macOS
} else {
// Fall back to earlier iOS and macOS APIs
}
νΉμ μμ
μ μννλ μ½λ λΈλ.
λ³΄ν΅ κ°μ κΈ°λ₯μ νλ©΄μ λλ² μ΄μ λ°λ³΅λλ κ²μ λ¬Άμ΄μ ν¨μλ‘ λ§λ λ€.
ν¨μ μ΄λ¦, μΈμ, λ°ν νμ , λ°νκ°μ΄ μλ ννλ‘ λ€λ₯Έ μΈμ΄μ ν¨μμ ꡬ쑰λ λμΌνκΈ° λλ¬Έμ, Swift ν¨μμ νΉμ΄ν μ λ§ μ 리νκ² λ€.
func greet(person name: String) -> String {
return "Hello, " + name + "!";
}
ν¨μλ₯Ό νΈμΆνλ©΄μ νλΌλ―Έν°λ₯Ό λκ²¨μ€ λ μ¬μ©νλ μ΄λ¦κ³Ό ν¨μ λ΄λΆμμ ν΄λΉ μΈμλ₯Ό μ¬μ©νλ μ΄λ¦μ΄ λ€λ₯΄λ€.
ν¨μ μΈλΆμμ νλΌλ―Έν° μ΄λ¦ λΌλ²¨μ μλ΅νκ³ μΆμΌλ©΄ _ λ₯Ό μ¬μ©νλ€.
π μλ μλλ ν€μλλ `parametername` μ²λΌ ` λ₯Ό λΆμ¬μ μ£Όλ©΄ λλΆλΆμ μ΄λ¦μ νλΌλ―Έν° μ΄λ¦μΌλ‘ μ¬μ©ν μ μλ€.
μ¬μ€ μ΄κ±΄ λ€λ₯Έ μΈμ΄μμλ κ°λ₯νμ§λ§, tuple μ λΌλ²¨μ μ€ μ μλ€λ κ²μ νΉμ΄νλ€.
κ·Έλ¦¬κ³ ννμ λν μ΅μ
λμ λ°νν μ μλ€λ건 νΉμ΄νλ€.
func minMax(array: [Int]) -> (min: Int, max: Int) {
var currentMin = array[0]
var currentMax = array[0]
for value in array[1..<array.count] {
if value < currentMin {
currentMin = value
} else if value > currentMax {
currentMax = value
}
}
return (currentMin, currentMax)
}
let bounds = minMax(array: [8, -6, 2, 109, 3, 71])
print("min is \(bounds.min) and max is \(bounds.max)")
// λ°ννλ ννμ κ°μ λΌλ²¨ μ΄λ¦μΌλ‘ μ¬μ©.
// Prints "min is -6 and max is 109"
func addTwo(first: Int, second: Int = 2) {
return first + second
}
addTwo(first: 1) // 3
addTwo(first: 10, second: 3) // 13
π κΈ°λ³Έ κ°μ΄ μλ 맀κ°λ³μλ₯Ό κΈ°λ³Έ κ°μ΄ μλ κ²λ³΄λ€ μμ λ°°μΉνλ κ²μ΄ μ’κ³ ,
κΈ°λ³Έ κ° μ¬λΆμ μκ΄μμ΄ λ μ€μν κ°μ μμ λ°°μΉνλ κ²μ΄ μ’λ€.
κ°μ νμ
μ νλΌλ―Έν°κ° μ¬λ¬κ° λ€μ΄μ¬ μ μμ λ,
μΈμ νμ
μ Type... μΌλ‘ μ§μ ν΄μ£Όλ©΄, ν΄λΉ νμ
μ 0κ° μ΄μμ μΈμκ° λ€μ΄μ¬ μ μλ€.
func arithmeticMean(_ numbers: Double...) -> Double {
var total: Double = 0
for number in numbers {
total += number
}
return total / Double(numbers.count)
}
arithmeticMean(1, 2, 3, 4, 5)
// returns 3.0, which is the arithmetic mean of these five numbers
arithmeticMean(3, 8.25, 18.75)
// returns 10.0, which is the arithmetic mean of these three numbers
ν¨μ μμμ 볡μ¬κ°μ΄ λ°λλκΉ, C μ ν¬μΈν°λ₯Ό μ΄μ©ν΄ μ£Όμκ°μ λ겨μ λ°κΎΈλ κ²μ²λΌ
inout ν€μλλ₯Ό λ£μ΄μ, ν¨μ λ°μ κ°μ΄ λ°λμ μκ² ν΄μ€λ€.
func swapTwoInts(_ a: inout Int, _ b: inout Int) {
let temporaryA = a
a = b
b = temporaryA
}
var num1 = 0
var num2 = 1
swapTwoInts(&num1, &num2)
π ν¨μν νλ‘κ·Έλλ° ν¨λ¬λ€μμμλ μ§μνλ κ²μ΄ μ’λ€.
-> Memory Safety λ₯Ό 보μ₯νμ§ μμ μ μμ.
π inout μ κΈ°λ³Έκ° X, κ°λ³ νλΌλ―Έν° X
ν¨μλ₯Ό type μΌλ‘ μ¬μ©ν μ μλ€.
func addTwoInts(_ a: Int, _ b: Int) -> Int {
return a + b
}
// λ³μμ ν¨μλ₯Ό ν λΉ κ°λ₯.
var mathFunction: (Int, Int) -> Int = addTwoInts
μΈμλ‘ ν¨μλ₯Ό λκ²¨μ€ μ μμ.
func printMathResult(_ mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
print("Result: \(mathFunction(a, b))")
}
printMathResult(addTwoInts, 3, 5)
// Prints "Result: 8"
ν¨μλ₯Ό λ°νν μλ μμ.
func stepForward(_ input: Int) -> Int {
return input + 1
}
func stepBackward(_ input: Int) -> Int {
return input - 1
}
func chooseStepFunction(backward: Bool) -> (Int) -> Int {
return backward ? stepBackward : stepForward
}
ν¨μ μμ ν¨μλ₯Ό μ μν μ μλ€. κ·Έλ¬λ©΄ κ·Έ ν¨μλ₯Ό μ¬μ©ν μ μλ λ²μλ 컀λ€λ ν¨μ μ!
func chooseStepFunction(backward: Bool) -> (Int) -> Int {
func stepForward(input: Int) -> Int { return input + 1 }
func stepBackward(input: Int) -> Int { return input - 1 }
return backward ? stepBackward : stepForward
}
fatalError
func crashAndBurn() -> Never {
fatalError("fatal error")
}
@discardableResult
@discardableResult func discardableResultSay(_ something: String) -> String {
print(something)
return something
}
discardableResultSay("hi") // λ°νκ°μ μ¬μ©νμ§ μμ§λ§ μ»΄νμΌλ¬ κ²½κ³ μλΈ.