์ ์ด๋ฌธ ( Control Flow )
์๋ ํ์ธ์ ์ค๋์ Swift์์ ์ ๊ณตํ๋ ์ ์ด๋ฌธ์ ๊ดํ์ฌ ์ ์ด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค ๐คจ
Swift์์ ์ ๊ณตํ๋ ์ ์ด๋ฌธ์
- while loop
- if guard
- switch
- for-in
For-in ๋ฌธ ( For - In - Loops )
for-in ๋ฌธ์ ๋ฐฐ์ด, ์ซ์, ๋ฌธ์์ด์ ์์๋๋ก ์ํํ๊ธฐ ์ํด ์ฌ์ฉํฉ๋๋ค.
let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
print("Hello \(name)!")
}
// Hello Anna!
// Hello Alex!
// Hello Brian!
// Hello Jack!
Dictionary์์ ๋ฐํ๋ ํค-๊ฐ ์์ผ๋ก ๊ตฌ์ฑ๋ ํํ์ ์ํํ๋ฉฐ ์ ์ดํ ์ ์์ต๋๋ค.
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {
print("\(animalName)s have \(legCount) legs")
}
// ants have 6 legs
// spiders have 8 legs
// cats have 4 legs
Dictionaryํ์ ์์๊ฐ ์ง์ ๋์ง ์์ผ๋ฉฐ ๋ฐ๋ณตํ๋ค๊ณ ํด์ ๊ฒ์๋๋ ์์๊ฐ ๋ณด์ฅ๋์ง ์์ต๋๋ค.
์๋์ ์ฝ๋๋ฅผ ํตํด ์ซ์ ๋ฒ์๋ฅผ ์ง์ ํด ์ํํ ์ ์์ต๋๋ค.
for index in 1...5 {
print("\(index) times 5 is \(index * 5)")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
์์๋๋ก ์ ์ดํ ํ์ํ์ง ์์ ๊ฒฝ์ฐ ๋ณ์ ์ด๋ฆ ๋์ _ ์ ์ฌ์ฉํ์ฌ ๊ฐ์ ๋ฌด์ํ ์ ์์ผ๋ฉฐ ์ฑ๋ฅ์ ๋์ผ ์ ์์ต๋๋ค.
_ ๋ฅผ ์ฌ์ฉํ๋ฉด ์ฝ๋๋ ๋ฃจํ๋ฅผ ํ์๋งํผ ์คํํ๋ฉฐ ๊ฐ๋ณ ๊ฐ์ด ๋ฌด์๋ฉ๋๋ค.
let base = 3
let power = 10
var answer = 1
for _ in 1...power {
answer *= base
}
print("\(base) to the power of \(power) is \(answer)")
// Prints "3 to the power of 10 is 59049"
๋ฒ์ ์ฐ์ฐ์๋ก ์ฌ์ฉ๋ฒ์ ๋๋ค.
let minutes = 60
for tickMark in 0..<minutes {
// render the tick mark each minute (60 times)
}
//0๋ถํฐ 59๊น์ง ์ด 60๋ฒ ๋ฐ๋ณต
stride(from:to:by:) ํจ์๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.
stride(from:to:by:) : ์ง์ ๋ ์๋งํผ ๋จ๊ณ์ ์ผ๋ก ์์ ๊ฐ์์ ๋ ๊ฐ๊น์ง์ ์ํ์ค๋ฅผ ๋ฐํํฉ๋๋ค.
๋งค๊ฐ๋ณ์๋ก๋
- from start - ์ํ์ค์ ์ฌ์ฉํ ์์ ๊ฐ์ ๋๋ค. ์ํ์ค์ ๊ฐ์ด ํฌํจ๋ ๊ฒฝ์ฐ ์ฒซ ๋ฒ์จฐ ๊ฐ์ start์ ๋๋ค.
- to end - ์ํ์ค๋ฅผ ์ ํํ๋ ๋ ๊ฐ์ ๋๋ค. end ๊ฒฐ๊ณผ ์ํ์ค์ ์์๊ฐ ์๋๋๋ค.
- by stride - ๊ฐ ๋ฐ๋ณต์์ ๋จ๊ณ๋ณ๋ก ์ํํ ์์ ๋๋ค. ์์๋ stride์์ชฝ์ผ๋ก ๋ฐ๋ณต๋ฉ๋๋ค. ์์๋ stride์๋์ชฝ์ผ๋ก ๋ฐ๋ณต๋ฉ๋๋ค.
let minuteInterval = 5
for tickMark in stride(from: 0, to: minutes, by: minuteInterval) {
// render the tick mark every 5 minutes (0, 5, 10, 15 ... 45, 50, 55)
}
//0๋ถํฐ 5์ฉ ์ฆ๊ฐํ์ฌ minutes๊น์ง ๋ฐ๋ณต์ํฉ๋๋ค. minutes๊ฐ์ 60์ด๋ฏ๋ก 55๊น์ง ํด๋น๋ฉ๋๋ค.
//stride(from:through:by:)์ ์ฌ์ฉํ๋ฉด ๋ซํ ๋ฒ์๋ฅผ ์ฌ์ฉํ ์๋ ์์ต๋๋ค.
let hours = 12
let hourInterval = 3
for tickMark in stride(from: 3, through: hours, by: hourInterval) {
// render the tick mark every 3 hours (3, 6, 9, 12)
}
While
While๋ฌธ์ ์กฐ๊ฑด์ด ๊ฑฐ์ง์ผ ๋๊น์ง ๊ตฌ๋ฌธ์ ๋ฐ๋ณตํฉ๋๋ค.
while ์กฐ๊ฑด {
code
}
let value = true
while value {
print("๋ฌดํ์ผ๋ก ๋ฐ๋ณต๋ฉ๋๋ค")
}
var num = 0
while num < 5 {
print( " \(num) times ")
num += 1
}
// 0 times
// 1 times
// 2 times
// 3 times
// 4 times
Repeat-While ๋ฌธ
repeat-While๋ฌธ์ ๋ค๋ฅธ ์ธ์ด์ do-while๋ฌธ๊ณผ ์ ์ฌํ while๋ฌธ์ ๋๋ค.
๊ตฌ๋ฌธ์ ์ต์ํ ๋ฒ ์คํํ๊ณ while์กฐ๊ฑด์ด ๊ฑฐ์ง์ผ ๋๊น์ง ๋ฐ๋ณตํฉ๋๋ค.
var num = 1
let value = true
repeat {
print(num)
} while value
//repeat while๋ฌธ์ ์ต์ ํ๋ฒ์ ์คํ๋์ง๋ง ์กฐ๊ฑด์ด true์ด๋ฏ๋ก ๋ฌดํ์ผ๋ก ๋ฐ๋ณต๋ฉ๋๋ค.
var a = 5
var b = 10
repeat {
a += 1
print( " number a is increased to \(a) " )
} while a < b
// number a is increased to 6
// number a is increased to 7
// number a is increased to 8
// number a is increased to 9
// number a is increased to 10
์กฐ๊ฑด์ ๊ตฌ๋ฌธ ( Conditional Statements )
Swift์์๋ if ์ switch๋ฌธ ๋ ๊ฐ์ง ์กฐ๊ฑด ๊ตฌ๋ฌธ์ ์ ๊ณตํฉ๋๋ค.
If ๋ฌธ
if๋ฌธ์ ์ด๋ ํ ๊ฐ์ด ํน์ ์กฐ๊ฑด์ ํด๋นํ ๋๋ง ์ ํ์ ์ผ๋ก ์ฝ๋๋ฅผ ์คํํฉ๋๋ค.
if๋ง ์ฌ์ฉํ์ ๋
var temperatureInFahrenheit = 30
if temperatureInFahrenheit <= 32 {
print("It's very cold. Consider wearing a scarf.")
}
// Prints "It's very cold. Consider wearing a scarf."
//์กฐ๊ฑด์ ๋ง์กฑํ๋ฏ๋ก ์ถ๋ ฅ๋ฉ๋๋ค.
if else๋ฅผ ์ฌ์ฉํ์ ๋
temperatureInFahrenheit = 40
if temperatureInFahrenheit <= 32 {
print("It's very cold. Consider wearing a scarf.")
} else {
print("It's not that cold. Wear a t-shirt.")
}
// Prints "It's not that cold. Wear a t-shirt."
// 40<=32 ์กฐ๊ฑด์ ๋ง์กฑํ์ง ์์ else ๊ตฌ๋ฌธ์ ์ถ๋ ฅํฉ๋๋ค.
if else else-if๋ฅผ ์ฌ์ฉํ์ ๋
temperatureInFahrenheit = 90
if temperatureInFahrenheit <= 32 {
print("It's very cold. Consider wearing a scarf.")
} else if temperatureInFahrenheit >= 86 {
print("It's really warm. Don't forget to wear sunscreen.")
} else {
print("It's not that cold. Wear a t-shirt.")
}
// Prints "It's really warm. Don't forget to wear sunscreen."
//else-if ์กฐ๊ฑด์ธ 90>=86 ์กฐ๊ฑด์ ๋ง์ ๊ตฌ๋ฌธ์ ์คํํฉ๋๋ค.
if else-if๋ฅผ ์ฌ์ฉํ์ ๋
temperatureInFahrenheit = 72
if temperatureInFahrenheit <= 32 {
print("It's very cold. Consider wearing a scarf.")
} else if temperatureInFahrenheit >= 86 {
print("It's really warm. Don't forget to wear sunscreen.")
}
// ๋ง๋ ์กฐ๊ฑด์ ์์ด ์๋ฌด๊ฒ๋ ์ถ๋ ฅ ๋์ง ์์ต๋๋ค.
Switch
break๊ฐ ํ์์ ์ด์ง ์์ง๋ง case ์์ ํน์ ์ง์ ์์ ๋ฉ์ถ๋๋ก ํ๊ธฐ ์ํด break์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
case์์๋ ์ต์ ํ๋์ ์คํ ๊ตฌ๋ฌธ์ด ๋ฐ๋์ ์์ด์ผ ํฉ๋๋ค.
switch๋ฌธ์ ๊ธฐ๋ณธ ํํ์ ๋๋ค.
switch some value to consider {
case value 1:
respond to value 1
case value 2,
value 3:
respond to value 2 or 3
default:
otherwise, do something else
}
๋ฌธ์๋ฅผ ๋น๊ตํ๊ธฐ ์ํด switch๋ฌธ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
let someCharacter: Character = "z"
switch someCharacter {
case "a":
print("The first letter of the alphabet")
case "z":
print("The last letter of the alphabet")
default:
print("Some other character")
}
// Prints "The last letter of the alphabet"
์์์ ๋งํ๋ค์ํผ switch๊ตฌ๋ฌธ์ ์์์ ์ธ ์งํ์ ์ฌ์ฉํ์ง ์์ต๋๋ค.
switch๊ตฌ๋ฌธ์ด ๊ธฐ๋ณธ์ ์ผ๋ก ๋ชจ๋ case๋ฅผ ์ํํ์ฌ default๋ฅผ ๋ง๋ ๋๊น์ง ์งํ๋ฉ๋๋ค.
let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a":
case "A":
print("The letter A")
default:
print("Not the letter A")
}
//case์์๋ ์ต์ ํ๋์ ์คํ ๊ตฌ๋ฌธ์ด ๋ฐ๋์ ์์ด์ผ ํฉ๋๋ค.
// ์ปดํ์ผ ์๋ฌ ๋ฐ์!
์ฝ๋๋ฅผ ์ข ๋ ํจ์จ์ ์ผ๋ก ์์ฑํ ์ ์์ต๋๋ค. case ์์ ์ฝค๋ง ( , )๋ก ๊ตฌ๋ถํ์ฌ case ์กฐ๊ฑด์ ํผํฉํ์ฌ ์ฌ์ฉํ ์ ์์ต๋๋ค.
let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a", "A":
print("The letter A")
default:
print("Not the letter A")
}
// Prints "The letter A"
์ธํฐ๋ฒ ๋งค์นญ ( Interval Matching )
์ซ์์ ํน์ ๋ฒ์๋ฅผ ์กฐ๊ฑด์ผ๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค.
let approximateCount = 62
let countedThings = "moons orbiting Saturn"
let naturalCount: String
switch approximateCount {
case 0:
naturalCount = "no"
case 1..<5:
naturalCount = "a few"
case 5..<12:
naturalCount = "several"
case 12..<100:
naturalCount = "dozens of"
case 100..<1000:
naturalCount = "hundreds of"
default:
naturalCount = "many"
}
print("There are \(naturalCount) \(countedThings).")
// ์กฐ๊ฑด์ธ 12..<100์ ๋ง์กฑํ์ฌ naturalCount์ "dozens of" ์ ์ธ๋ฉ๋๋ค.
// Prints "There are dozens of moons orbiting Saturn."
ํํ ( Tuple )
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) 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"
์กฐ๊ฑด์ธ ( -2...2 , -2...2 )์ ๋ง์กฑํ์ฌ ๊ตฌ๋ฌธ์ ์ถ๋ ฅํฉ๋๋ค.
Where๋ฌธ
where์ ์ฉ๋๋ก๋ ํน์ ํจํด๊ณผ ๊ฒฐํฉํ์ฌ ์กฐ๊ฑด ์ถ๊ฐ, ํ์ ์ ๋ํ ์ ์ฝ ์ถ๊ฐ๊ฐ ์์ต๋๋ค.
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
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 "(1, -1) is on the line x == -y"
ํผํฉ ์ผ์ด์ค ( Compound Cases )
case์ ์ฝค๋ง ( , )๋ก ๊ตฌ๋ถํ์ฌ ์ฌ๋ฌ ์กฐ๊ฑด์ ํผํฉํ์ฌ ์ฌ์ฉ ๊ฐ๋ฅํฉ๋๋ค.
let someCharacter: Character = "e"
switch someCharacter {
case "a", "e", "i", "o", "u":
print("\(someCharacter) is a vowel")
case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
"n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
print("\(someCharacter) is a consonant")
default:
print("\(someCharacter) is not a vowel or a consonant")
}
// Prints "e is a vowel"
๊ฐ-๋ฐ์ธ๋ฉ์์๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.
let stillAnotherPoint = (9, 0)
switch stillAnotherPoint {
case (let distance, 0), (0, let distance):
print("On an axis, \(distance) from the origin")
default:
print("Not on an axis")
}
// Prints "On an axis, 9 from the origin"
์ ์ด ์ ์ก ๊ตฌ๋ฌธ ( Control Transfer Statements )
์ฝ๋์ ์งํ์ ๊ณ์ํ ์ง ๋ง์ง๋ฅผ ๊ฒฐ์ ํ๊ฑฐ๋ ์คํ๋๋ ์ฝ๋์ ํ๋ฆ์ ๋ฐ๊พธ๊ธฐ ์ํด ์ฌ์ฉํฉ๋๋ค.
- continue
- break
- fallthrough
- return
- throw
continue๋ฌธ
continue๋ฌธ์ ํ์ฌ loop๋ฅผ ์ค์งํ๊ณ ๋ค์ loop๋ฅผ ์ํํ๋๋ก ํฉ๋๋ค.
let puzzleInput = "great minds think alike"
var puzzleOutput = ""
let charactersToRemove: [Character] = ["a", "e", "i", "o", "u", " "]
for character in puzzleInput {
if charactersToRemove.contains(character) {
continue
} else {
puzzleOutput.append(character)
}
}
print(puzzleOutput)
// Prints "grtmndsthnklk"
break๋ฌธ
break๋ฌธ์ ์ ์ฒด ์ ์ด๋ฌธ์ ์คํ์ ์ฆ๊ฐ ์ค์ง์ํต๋๋ค. break๋ฌธ์ loop๋ switch๋ฌธ์์ ์ฌ์ฉ ๊ฐ๋ฅํฉ๋๋ค.
let numberSymbol: Character = "ไธ" // ์ค๊ตญ์ด๋ก 3์ ์๋ฏธํ๋ ๋ฌธ์์
๋๋ค.
var possibleIntegerValue: Int?
switch numberSymbol {
case "1", "ูก", "ไธ", "เน":
possibleIntegerValue = 1
case "2", "ูข", "ไบ", "เน":
possibleIntegerValue = 2
case "3", "ูฃ", "ไธ", "เน":
possibleIntegerValue = 3
case "4", "ูค", "ๅ", "เน":
possibleIntegerValue = 4
default:
break
}
if let integerValue = possibleIntegerValue {
print("The integer value of \(numberSymbol) is \(integerValue).")
} else {
print("An integer value could not be found for \(numberSymbol).")
}
fallthrough๋ฌธ
fallthroughํค์๋๋ ์ดํ์ case์ ๋ํด์๋ ์คํํ๊ฒ ํฉ๋๋ค. Swift์์๋ ์กฐ๊ฑด์ ๋ง๋ case์ ์คํํ๋ฉด switch๋ฌธ์ ์ข ๋ฃ๋ฉ๋๋ค. ํ์ง๋ง fallthrough๋ฅผ ์ฌ์ฉํ๋ฉด ์๋์ผ๋ก ์ข ๋ฃ๋๋ ๊ฒ์ ๋ง์ต๋๋ค.
let integerToDescribe = 5
var description = "The number \(integerToDescribe) is"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
description += " a prime number, and also"
fallthrough
default:
description += " an integer."
}
print(description)
// Prints "The number 5 is a prime number, and also an integer."
โป fallthrough๋ case ์กฐ๊ฑด์ ํ์ธํ์ง ์๊ณ ๊ทธ๋ฅ ๋ค์ case๋ฅผ ์คํํ๊ฒ ๋ง๋ญ๋๋ค.
๊ตฌ๋ฌธ ๋ ์ด๋ธ
๋ฐ๋ณต๋ฌธ์ด๋ ์กฐ๊ฑด๋ฌธ ๋ฑ ํน์ ๊ตฌ๋ฌธ์ ๋ ์ด๋ธ์ ๋ถ์ฌ ๊ธฐ์ตํ๊ฒ ๋ง๋ค๋ฉฐ break, continue๊ตฌ๋ฌธ์ด ์ฌ์ฉ๋ ๋ ๋ง๋ ๋ ์ด๋ธ์ ๋ช ์ํด์ค์ผ๋ก์จ ์ํ๋ ๊ตฌ๋ฌธ ์์น์ ์ ํํ ํ๋ฆ ์ ์ด๊ฐ ๋ ์ ์๋๋ก ํ๋ ๋ฌธ๋ฒ์ ๋๋ค.
label name: while condition {
statements
}
aLable : for i in 1..<10{
bLable : for j in 1..<5{
if( j == 4 ){
break aLable
}
print(" i:\(i) j:\(j) ")
}
}
// i:1 j:1
// i:1 j:2
// i:1 j:3
์ด๋ฅธ ํ์ถ ( Early Exit )
guard๋ฌธ์ ์ด์ฉํ์ฌ ํน์ ์กฐ๊ฑด์ ๋ง์กฑํ์ง ์์ผ๋ฉด ์ดํ ์ฝ๋๋ฅผ ์คํํ์ง ์๋๋ก ์์ฑํ ์ ์์ต๋๋ค.
func greet(person: [String: String]) {
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).")
}
greet(person: ["name": "John"])
// Prints "Hello John!"
// Prints "I hope the weather is nice near you."
// ์ฒซ๋ฒ์งธ guard ์กฐ๊ฑด์ ๋ง์กฑํ์ฌ Prints "Hello John!"์ ์ถ๋ ฅํ๊ฒ ๋์์ผ๋ฉฐ
// ๋๋ฒ์จฐ guard ์กฐ๊ฑด์ ๋ง์กฑํ์ง ๋ชปํ์ฌ else ๊ตฌ๋ฌธ์ ์ถ๋ ฅํฉ๋๋ค.
greet(person: ["name": "Jane", "location": "Cupertino"])
// Prints "Hello Jane!"
// Prints "I hope the weather is nice in Cupertino."
// ์ฒซ๋ฒ์งธ guard ์กฐ๊ฑด์ ๋ง์กฑํ์ฌ Prints "Hello Jane!""์ ์ถ๋ ฅํ๊ฒ ๋์์ผ๋ฉฐ
// ๋๋ฒ์จฐ guard // Prints "I hope the weather is nice near you." ์ ์ถ๋ ฅํ๊ฒ ๋ฉ๋๋ค.
์ด์ฉ ๊ฐ๋ฅํ API ๋ฒ์ ํ์ธ ( Checking API Availability )
Swift์์๋ ๊ธฐ๋ณธ์ ์ผ๋ก ํน์ ํ๋ซํผ ( ios , macOS, tvOS, watchOS )๊ณผ ํน์ ๋ฒ์ ์ ํ์ธํ๋ ๊ตฌ๋ฌธ์ ์ ๊ณตํด์ค๋๋ค. ์ด ๊ตฌ๋ฌธ์ ํ์ฉํด ํน์ ํ๋ซํผ๊ณผ ๋ฒ์ ์ ์ฌ์ฉํ๋ ๊ธฐ๊ธฐ์ ๋ํ ์ฒ๋ฆฌ๋ฅผ ๋ฐ๋ก ํ ์ ์์ต๋๋ค.
if #available(platform name version, ..., *) {
statements to execute if the APIs are available
} else {
fallback statements to execute if the APIs are unavailable
}
//์ค์ ์ฌ์ฉ ์์
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์์ ์ ๊ณตํ๋ ์ ์ด๋ฌธ์ ๊ดํ์ฌ ์ ๋ฆฌ๋ฅผ ํด๋ดค์ต๋๋ค. ์ ๋ฆฌ๋ฅผ ํ๋ฉด์ ๊ตฌ๋ฌธ ๋ ์ด๋ธ๋ผ๋ ๊ฒ์ ๋ํด์ ์ฒ์ ์๊ฒ ๋์ด
๋ค์ ํ๋ฒ ๋ด์ฉ์ ๊ณต๋ถํด์ผ ํ ๊บผ๊ฐ์ต๋๋คใ ใ