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") // ๋ฐํ๊ฐ์ ์ฌ์ฉํ์ง ์์ง๋ง ์ปดํ์ผ๋ฌ ๊ฒฝ๊ณ ์๋ธ.