๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป Programming ๊ฐœ๋ฐœ/๐ŸŽ iOS ๊ฐœ๋ฐœ, Swift

[Swift] ํ๋ฆ„์ œ์–ด๊ตฌ๋ฌธ

by kimdee 2022. 1. 18.
๋ฐ˜์‘ํ˜•

 

์ž‘๋…„๋„์—  ๋…ธ์…˜์œผ๋กœ ์ •๋ฆฌํ–ˆ๋˜ ๊ธ€์„ ํ‹ฐ์Šคํ† ๋ฆฌ๋กœ ์˜ฎ๊ฒผ์Šต๋‹ˆ๋‹ค. ๐Ÿ“š๊ผผ๊ผผํ•œ ์žฌ์€์”จ์˜ ์Šค์œ„ํ”„ํŠธ: ๋ฌธ๋ฒ•ํŽธ ๊ณผ Swift ๋ฌธ์„œ๋ฅผ ๋ณด๋ฉด์„œ ๋”ฐ๋กœ ์ •๋ฆฌํ•˜์—ฌ ๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์ด์—์š”. ์ด๋ฒˆ์—  Notion2Tistory ๋ฅผ ์‚ฌ์šฉํ•ด๋ดค๋Š”๋ฐ, ์ฝ”๋“œ๋ธ”๋Ÿญ์€ ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์‚ฌ๋ผ์ง€๊ณ  ์ด๋ฏธ์ง€๊ฐ€ ๋‹ค ๊นจ์ ธ์„œ html ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด์„œ ์ˆ˜์ •ํ•ด์•ผ ๋˜๋”๋ผ๊ตฌ์š”. ๋งŒ์•ฝ  ๋…ธ์…˜์—์„œ ๋งˆํฌ๋‹ค์šด๋งŒ ๋‹ค๋ฃจ์‹œ๋Š” ๊ฒฝ์šฐ(e.g.Table of Contents, Heading, etc) ๋ฐ”๋กœ  ํ‹ฐ์Šคํ† ๋ฆฌ๋กœ ๊ธ€์„ ์˜ฌ๋ฆฌ๊ธฐ ์ข‹์•„์š”. ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค. 

 

    

๋ชฉ์ฐจ 

 

0. ์‹œ์ž‘ํ•˜๋ฉฐ

 

ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ณผ์ •์—์„œ ์ž‘์„ฑํ•˜๋Š” ์†Œ์Šค์ฝ”๋“œ : ๊ตฌ๋ฌธ(Statement)

 

  • ๋‹จ์ˆœ ๊ตฌ๋ฌธ

    ์‹, ๊ฐ’ ํ‘œํ˜„, ๊ฐ์ข… ๊ฐ์ฒด์˜ ์„ ์–ธ, ์ •์˜ ๋“ฑ์— ์‚ฌ์šฉ๋˜๋Š” ๊ตฌ๋ฌธ.

    ์—ฐ์‚ฐ์ฒ˜๋ฆฌ ๋“ฑ๋„ ๋ชจ๋‘ ๋‹จ์ˆœ ๊ตฌ๋ฌธ์— ์†ํ•จ.

    //๋ณ€์ˆ˜ ๋˜๋Š” ์ƒ์ˆ˜ ์„ ์–ธ 
      let number = 4
      var cat = """ ์šฐ๋ฆฌ ์ง‘ ๊ณ ์–‘์ด ์ด๋ฆ„์€ ๊น€์‚ฐ์ž…๋‹ˆ๋‹ค. ๋งค์šฐ ๊ท€์—ฌ์›Œ์š”.  """
  • ํ๋ฆ„์ œ์–ด ๊ตฌ๋ฌธ

    ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ๊ณผ์ •์—์„œ ์‹คํ–‰ ํ๋ฆ„์„ ๋Šฅ๋™์ ์œผ๋กœ ์ œ์–ดํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์ .

    ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰๋  ์ผ๋ถ€ ์‹คํ–‰๊ณผ์ •์„ ๊ฑด๋„ˆ๋›ฐ๊ฑฐ๋‚˜ ๋˜๋Œ์•„์˜ค๋„๋ก, ๋˜๋Š” ๋ฐ˜๋ณต์ ์œผ๋กœ ์‹คํ–‰๋˜๋„๋ก ํ๋ฆ„์„ ์ œ์–ด.

 

๐Ÿ’ก
์Šค์œ„ํ”„ํŠธ์˜ ํ๋ฆ„ ์ œ์–ด ๊ตฌ๋ฌธ์€ ํฌ๊ฒŒ 3๊ฐ€์ง€ 1. ๋ฐ˜๋ณต๋ฌธ Loop Statements 2. ์กฐ๊ฑด๋ฌธ Conditional Statements 3. ์ œ์–ด ์ „๋‹ฌ๋ฌธ Control Transfer Statements

 

ํ๋ฆ„์ œ์–ด ๊ตฌ๋ฌธ์—์„œ๋Š” { ์ฝ”๋“œ ๋ธ”๋ก }์„ ์ง€์ •ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค.

 

1. ๋ฐ˜๋ณต๋ฌธ (Loop)

๋ฃจํ”„ ํšŸ์ˆ˜๊ฐ€ ์ •ํ•ด์ ธ ์žˆ๋Š”์ง€๋ฅผ ๊ธฐ์ค€์œผ๋กœ for, while

for : ํšŸ์ˆ˜์— ์˜ํ•œ ๋ฐ˜๋ณต

while : ์กฐ๊ฑด์— ์˜ํ•œ ๋ฐ˜๋ณต

for~in

for <๋ฃจํ”„ ์ƒ์ˆ˜> in <์ˆœํšŒ ๋Œ€์ƒ> { <์‹คํ–‰ํ•  ๊ตฌ๋ฌธ> // ์ฝ”๋“œ๋ธ”๋ก ์‚ฌ์ด์— ์ž‘์„ฑ๋˜์–ด์•ผ ํ•จ  }
for <๋ฃจํ”„ ์ƒ์ˆ˜> in <์ˆœํšŒ ๋Œ€์ƒ> { 	
    	<์‹คํ–‰ํ•  ๊ตฌ๋ฌธ> 
    	// ์ฝ”๋“œ๋ธ”๋ก ์‚ฌ์ด์— ์ž‘์„ฑ๋˜์–ด์•ผ ํ•จ  
}

 

์ˆœํšŒ๋Œ€์ƒ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…

  • ๋ฐฐ์—ด Array
  • ๋”•์…”๋„ˆ๋ฆฌ Dictionary
  • ์ง‘ํ•ฉ Set = Collective Types = Container Type
  • ๋ฒ”์œ„ ๋ฐ์ดํ„ฐ (๋ฒ”์œ„ ์—ฐ์‚ฐ์ž '...' ์— ์˜ํ•ด ๊ทœ์น™์ ์ธ ๊ฐ„๊ฒฉ์œผ๋กœ ๋‚˜์—ด๋œ ์ •์ˆ˜๋“ค ๋ชจ์Œ)
  • ๋ฌธ์ž์—ด String

์ˆœํšŒ๋Œ€์ƒ์€ ์ฃผ๋กœ ์ˆœ๋ฒˆ์„ ๊ฐ€์ง€๋Š” ์ง‘๋‹จ ์ž๋ฃŒํ˜•์ด๋‚˜ ๋ฒ”์œ„๋ฅผ ๊ฐ€์ง€๋Š” ๋ฐ์ดํ„ฐ ๋“ฑ์ด ์‚ฌ์šฉ๋จ.

 

๐Ÿ’ก
for~in ๊ตฌ๋ฌธ์„ ์ด๋ฃจ๋Š” ๋ฃจํ”„์ƒ์ˆ˜๋Š” ๊ตฌ๋ฌธ์ด ๋ฐ˜๋ณต๋  ๋•Œ๋งˆ๋‹ค ์ˆœํšŒ๋Œ€์ƒ์ด ํฌํ•จํ•˜๋Š” ๊ฐœ๋ณ„์•„์ดํ…œ์„ ์ฐจ๋ก€๋กœ ๋„˜๊ฒจ๋ฐ›์•„ ์ž„์˜๋กœ ์ €์žฅ, ์‹คํ–‰ ๋ธ”๋ก๋‚ด์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ์—ญํ• . ๋ฃจํ”„์ƒ์ˆ˜๋Š” ๋ฃจํ”„ ๊ตฌ๋ฌธ์ด ์ˆœํšŒํ•  ๋•Œ๋งˆ๋‹ค ์ž๋™์œผ๋กœ ์žฌ์„ ์–ธ ๋˜๋ฏ€๋กœ let ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ง์ ‘ ์„ ์–ธํ•  ํ•„์š”๊ฐ€ ์—†์Œ. ์ •์˜๋œ ๋ฃจํ”„์ƒ์ˆ˜๋Š” ์˜ค์ง for~in ๊ตฌ๋ฌธ์˜ ์‹คํ–‰๋ธ”๋ก ๋‚ด๋ถ€์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ.

 

๐Ÿ˜Ž ๋ฌธ์ž์—ด ์ˆœํšŒ ๋ฐฉ๋ฒ•

String ์€ ๋‹จ์ผ ๊ฐ์ฒด๋กœ ์‚ฌ์šฉ๋˜๋‚˜, ๊ทธ ์•ˆ์€ Character ํƒ€์ž…์˜ ๊ฐœ๋ณ„ ๋ฌธ์ž๋“ค์ด ๋ชจ์—ฌ ์ด๋ฃจ์–ด์ง„ ๊ฐ์ฒด

String ํƒ€์ž… ์ž์ฒด๋Š” ์ˆœํšŒ์ฒ˜๋ฆฌ๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ, characters ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์•ผํ•จ.

var lang = "swift" 
for char in lang.characters { print("\char") }
[์‹คํ–‰๊ฒฐ๊ณผ] s w i f t

 

๐Ÿค  ๋ฃจํ”„ ์ƒ์ˆ˜์˜ ์ƒ๋žต _

์ˆœํšŒ๋Œ€์ƒ ์ž์ฒด๋ณด๋‹ค ์ˆœํšŒ๋Œ€์ƒ์˜ ํฌ๊ธฐ๋งŒํผ ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒƒ์ด ๋ชฉ์ ์ผ ๋•Œ, ์–ธ๋”๋ฐ” _ ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฃจํ”„์ƒ์ˆ˜๋ฅผ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์Œ.

let size = 5 
let padChar = "0" 
var keyword = "6"  
for _ in 1...size { 	
	keyword = padChar + keyword  
}

print("\(keyword)") // ์‹คํ–‰๊ฒฐ๊ณผ 000006

 

๐Ÿ˜‡ for~in ๊ตฌ๋ฌธ์˜ ์ค‘์ฒฉ

๋‹ค์ค‘ ๋ฃจํ”„ ๋˜๋Š” ๋ฉ€ํ‹ฐ๋ฃจํ”„

(์ •์‹ ๋ช…์นญ์ด ์•„๋‹˜)

 

while

์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋™์•ˆ ์‹คํ–‰.

while <์กฐ๊ฑด์‹> { 	<์‹คํ–‰ํ•  ๊ตฌ๋ฌธ> }

์กฐ๊ฑด์‹์€ ๋ฐ˜๋“œ์‹œ ์ฐธ์ด๋‚˜ ๊ฑฐ์ง“์„ ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•จ.

 

๐Ÿ˜Ž ๋ฌดํ•œ ๋ฃจํ”„
while true {     ...  }  /*  ๋ชจ๋ฐ”์ผ ์•ฑ ๋“ฑ ์‚ฌ์šฉ์ž ์•ก์…˜์„ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๋Œ€๊ธฐ ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•ด์•ผ๋˜๋Š” ๊ฒฝ์šฐ,  ์ด๋ฒคํŠธ ๋ฃจํ”„(=๋ฌดํ•œ๋ฃจํ”„)๋ฅผ ๋งŒ๋“ค์–ด ์‹คํ–‰ํ•จ.   Event-Driven Programming  : ์ˆœ์„œ๋Œ€๋กœ ์ˆ˜ํ–‰๋œ ํ›„ ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋˜๋Š” ๊ธฐ์กด ๋ฐฉ์‹์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด ์•„๋‹Œ ํŠน์ • ์ƒํ™ฉ์—์„œ ์‹คํ–‰ํ•  ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•ด๋‘๊ณ  ์ด๋ฅผ ์‚ฌ์šฉ์ž ์ด๋ฒคํŠธ์™€ ์—ฐ๊ฒฐํ•˜์—ฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•˜๋Š” ๋ฐฉ์‹.  */

repeat~while

ํƒ€ ์–ธ์–ด์˜ do-while ๊ตฌ๋ฌธ์— ํ•ด๋‹น.

  • ์Šค์œ„ํ”„ํŠธ 2.0 ๋ฒ„์ „๋ถ€ํ„ฐ ์ƒˆ๋กœ ์ถ”๊ฐ€๋˜์—ˆ์Œ.

    ๊ธฐ์กด์— ์‚ฌ์šฉ๋˜๋˜ do~while ๊ตฌ๋ฌธ์ด 2.0 ๋ฒ„์ „์—์„œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ๊ตฌ๋ฌธ์œผ๋กœ ๋ณ€๊ฒฝ๋˜๊ณ 

    ๊ทธ ๋Œ€์‹  repeat~while ๊ตฌ๋ฌธ์ด ์ œ๊ณต๋˜๊ธฐ ์‹œ์ž‘ํ•จ.

repeat { 	<์‹คํ–‰ํ•  ๊ตฌ๋ฌธ> } while <์กฐ๊ฑด์‹>

 

 

2. ์กฐ๊ฑด๋ฌธ

๋ถ„๊ธฐ๋ฌธ (Branch Statements)

ํ”„๋กœ๊ทธ๋žจ์—์„œ ํ•˜๋‚˜ ๋˜๋Š” ๊ทธ ์ด์ƒ์˜ ์กฐ๊ฑด๊ฐ’์— ๋”ฐ๋ผ ํŠน์ • ๊ตฌ๋ฌธ์„ ์‹คํ–‰ํ•˜๋„๋ก ํ”„๋กœ๊ทธ๋žจ ํ๋ฆ„์„ ๋ถ„๊ธฐํ•˜๋Š” ์—ญํ• .

 

๐Ÿ’ก
Swift์—์„œ ์ œ๊ณตํ•˜๋Š” ์กฐ๊ฑด๋ฌธ์€ 3๊ฐ€์ง€ 1. if 2. guard 3. switch

if

if ๋‹ค์Œ์˜ ์กฐ๊ฑด์‹์€ ๋ฐ˜๋“œ์‹œ Bool ํƒ€์ž…์˜ ์ฐธ, ๊ฑฐ์ง“์„ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ์ด์–ด์•ผ.

๊ณผ๊ฑฐ C ์Šคํƒ€์ผ์—์„œ ์ฐธ, ๊ฑฐ์ง“ ๋Œ€์‹ ์— ์ˆซ์ž 0, 1์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ์ง€๋งŒ Swift์—์„œ๋Š” ํ—ˆ์šฉํ•˜์ง€ ์•Š์Œ.

(์†Œ๊ด„ํ˜ธ)๋ฅผ ์ด์šฉํ•˜์—ฌ ์กฐ๊ฑด์‹์„ ๊ฐ์‹ธ์ฃผ์–ด๋„ ๋˜์ง€๋งŒ ํ•„์ˆ˜์‚ฌํ•ญ์€ ์•„๋‹˜.

 

// if ๊ตฌ๋ฌธ 
if <์กฐ๊ฑด์‹> { <์‹คํ–‰ํ•  ๊ตฌ๋ฌธ> }  
// if~else  
if <์กฐ๊ฑด์‹> { <์กฐ๊ฑด์‹์ด ์ฐธ์ผ ๋•Œ ์‹คํ–‰ํ•  ๊ตฌ๋ฌธ> } 
else { <์กฐ๊ฑด์‹์ด ๊ฑฐ์ง“์ผ ๋•Œ ์‹คํ–‰ํ•  ๊ตฌ๋ฌธ> }  
// if~else if 
if <์กฐ๊ฑด์‹ 1> { <์กฐ๊ฑด์‹ 1๊ฐ€ ์ฐธ์ผ ๋•Œ ์‹คํ–‰ํ•  ๊ตฌ๋ฌธ> } 
else if <์กฐ๊ฑด์‹ 2> { <์กฐ๊ฑด์‹ 2๊ฐ€ ์ฐธ์ผ ๋•Œ ์‹คํ–‰ํ•  ๊ตฌ๋ฌธ> } 
else { <์œ„ ์กฐ๊ฑด์„ ๋ชจ๋‘ ๋งŒ์กฑํ•˜์ง€ ์•Š์•˜์„ ๋•Œ ์‹คํ–‰ํ•  ๊ตฌ๋ฌธ> }

 

guard

 

if๋ฌธ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ฃผ์–ด์ง„ ํ‘œํ˜„์‹์˜ ๊ฒฐ๊ณผ๊ฐ€ ์ฐธ, ๊ฑฐ์ง“์ด๋ƒ์— ๋”ฐ๋ผ ๊ตฌ๋ฌธ์˜ ์‹คํ–‰ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •์ง“๋Š” ๋ฐฉ์‹.

๋‹จ guard ๊ตฌ๋ฌธ์€ else ๋ธ”๋ก์ด ํ•„์ˆ˜. ๋˜ํ•œ ์ฐธ์ผ ๋•Œ ์‹คํ–‰๋˜๋Š” ๋ธ”๋ก์ด ์—†๋‹ค.

 

guard <์กฐ๊ฑด์‹ ๋˜๋Š” ํ‘œํ˜„์‹> else { <์กฐ๊ฑด์‹ ๋˜๋Š” ํ‘œํ˜„์‹ ๊ฒฐ๊ณผ๊ฐ€ false์ผ ๋•Œ ์‹คํ–‰๋  ์ฝ”๋“œ> }

ํ›„์† ์ฝ”๋“œ๋“ค์ด ์‹คํ–‰๋˜๊ธฐ ์ „ ํŠน์ •์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์šฉ๋„.

ํŠน์ •์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ์•Š์€ ์ฑ„ ํ›„์†์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์‹ฌ๊ฐํ•œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ์— ์ „์ฒด๊ตฌ๋ฌธ์„ ์กฐ๊ธฐ์ข…๋ฃŒ(Early Exit) ํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉ๋จ.

 

 

์กฐ๊ธฐ์ข…๋ฃŒํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์ 

 

๋•Œ๋ฌธ์— guard ๊ตฌ๋ฌธ์˜ else ๋ธ”๋ก์—์„œ๋Š” ์ดํ›„์˜ ์ฝ”๋“œ์ง„ํ–‰์„ ๋ง‰์•„์ฃผ๋Š” ๊ตฌ๋ฌธ์ด ๋ฐ˜๋“œ์‹œ ํฌํ•จ๋˜์–ด์•ผ. (e.g. return, break, continue, throw, ๋˜๋Š” ๋ฐ˜ํ™˜๊ฐ’์ด ์—†๋Š” ํ•จ์ˆ˜๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. fatalError(_:file:line:)

 

ํ…Œ์ŠคํŠธ ์กฐ๊ฑด์ด ์ฐธ, ๊ฑฐ์ง“์ผ ๋•Œ ํ๋ฆ„

 

// Divide by Zero ์˜ค๋ฅ˜๋ฅผ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด guard ๊ตฌ๋ฌธ์„ ์ด์šฉํ•œ ํ•จ์ˆ˜ ์„ ์–ธ  
func divide(base: Int) {  	
	guard base != 0 
    else { 		
    	print("์—ฐ์‚ฐํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.") 		
    	return 	
    } 	
    let result = 100 / base 	
    print(result) 
}

 

guard ๊ตฌ๋ฌธ์€ ๋ณธ๋ž˜ ์‹คํ–‰ ํ๋ฆ„์„ ์ข…๋ฃŒํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์  : ์ฝ”๋“œ๋ฅผ ์ค‘์ฒฉํ•ด์„œ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Œ.

→ ์ฆ‰ 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." 
    greet(person: ["name": "Jane", "location": "Cupertino"]) 
    // Prints "Hello Jane!" 
    // Prints "I hope the weather is nice in Cupertino."

 

#available

OS ๋ฒ„์ „๋ณ„๋กœ ๊ตฌ๋ฌธ์„ ๋‚˜๋ˆ„์–ด ์ž‘์„ฑํ•ด์•ผ ํ•  ๋•Œ ์‚ฌ์šฉ.

e.g. ํŠน์ • API (์• ํ”Œ ๊ฐœ๋ฐœ์ž์šฉ API ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•ด์•ผ)

 

NSURLRequest

Swift 2 ๋ฒ„์ „๋ถ€ํ„ฐ ์ง€์›(built-in support)ํ•˜๊ธฐ ์‹œ์ž‘ํ•œ ๊ตฌ๋ฌธ.

 

์ด์ „์—๋Š” OS ๋ฒ„์ „์„ ์ถ”์ถœํ•˜๋Š” API๋ฅผ ์ง์ ‘ ํ˜ธ์ถœํ•˜์—ฌ, OS ๋ฒ„์ „์— ๋Œ€ํ•œ ๊ฐ’์„ ์–ป๊ณ  ์กฐ๊ฑด๋ฌธ์—์„œ ๋น„๊ต์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์„ ์ผ์–ด์•ผ.

#available

 

 

//Swift 2 ์ด์ „ ๋ฒ„์ „๊นŒ์ง€์˜ ๋ฐฉ์‹ 
import UIKit  
if(UIDevice.current().systemVersion.hasPrefix("9")) {  } 
else if(UIDevice.current().systemVersion.hasPrefix("8")) {  } 
else {  }

 

#available ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋” ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๊ตฌ๋ถ„์ด ๊ฐ€๋Šฅ

if #available( <ํ”Œ๋žซํผ์ด๋ฆ„ ๋ฒ„์ „>, <...>, <*> ){ 	<ํ•ด๋‹น๋ฒ„์ „์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” API ๊ตฌ๋ฌธ> }
else { 	<API๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ํ™˜๊ฒฝ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ> }

ํ˜ธ์ถœ์—ฐ์‚ฐ์ž( ) ๋ฅผ ํ†ตํ•ด ํ”Œ๋žซํผ ์ด๋ฆ„๊ณผ ๋ฒ„์ „ ๋“ฑ ์ธ์ž๊ฐ’์„ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Œ.

ํ”Œ๋žซํผ ์ด๋ฆ„๊ณผ ๋ฒ„์ „ ์‚ฌ์ด๋Š” ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„.

OSX 10.10

 

์ธ์ž๊ฐ’์€ ๊ฐ€๋ณ€์ธ์ž๋กœ ์ •์˜๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ์ž…๋ ฅ๊ฐฏ์ˆ˜์˜ ์ œํ•œ์ด ์—†์Œ.

์‰ผํ‘œ๋กœ๋งŒ ๊ตฌ๋ถ„ํ•˜์—ฌ ํ”Œ๋žซํผ ์ด๋ฆ„๊ณผ OS ๋ฒ„์ „์„ ๋‚˜์—ดํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋จ.

ํ”Œ๋žซํผ๊ณผ ๋ฒ„์ „์€ ์ƒ์ˆ˜๋กœ ์ธ์‹๋˜๋ฏ€๋กœ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๋”ฐ์˜ดํ‘œ๋Š” ํ•„์š”์—†์Œ.

๋‚˜์—ด์ด ๋๋‚˜๋ฉด ๋งˆ์ง€๋ง‰์€ * ๋กœ ๋งˆ๊ฐํ•˜์—ฌ ์ธ์ž๊ฐ’ ์ธ๋ ฅ์ด ๋ชจ๋‘ ๋๋‚ฌ์Œ์„ ์„ ์–ธ

 

if #available(iOS 9, OSX 10.11, watchOS 1, *) { 	 }
else { 	// API๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ–ˆ์„ ๋•Œ ์‹คํŒจ ์ฒ˜๋ฆฌ  }

 

๐Ÿค“ #available ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•  ํ”Œ๋žซํผ

  • ์•„์ดํฐ, ์•„์ดํŒจ๋“œ ๋“ฑ ํ„ฐ์น˜๊ธฐ๋ฐ˜ ์Šค๋งˆํŠธ๊ธฐ๊ธฐ์— ์‚ฌ์šฉ๋˜๋Š” iOS
  • ๋งฅ ์šฉ OSX
  • ์• ํ”Œ์‹œ๊ณ„ watchOS
  • ์• ํ”ŒTV tvOS

 

์ฐธ๊ณ 

Attributes - The Swift Programming Language (Swift 5.4)
There are two kinds of attributes in Swift-those that apply to declarations and those that apply to types. An attribute provides additional information about the declaration or type. For example, the discardableResult attribute on a function declaration indicates that, although the function returns a value, the compiler shouldn't generate a warning if the return value is unused.
https://docs.swift.org/swift-book/ReferenceManual/Attributes.html#ID348

 

๋ฉ”์ด์ € ๋ฒ„์ „( iOS 8, macOS 10.10 ๋“ฑ) ์„ ๋ช…๊ธฐํ•  ๋ฟ ์•„๋‹ˆ๋ผ ๋งˆ์ด๋„ˆ ๋ฒ„์ „๊นŒ์ง€๋„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. (iOS 11.2.5, macOS 10.13.3

 

 

 

switch

์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์„ ํŒจํ„ด์„ ๋น„๊ตํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์‹คํ–‰๋ธ”๋ก์„ ๊ฒฐ์ •ํ•˜๋Š” ์กฐ๊ฑด๋ฌธ.

switch <๋น„๊ต๋Œ€์ƒ> {  	
	case <๋น„๊ตํŒจํ„ด1> : 		
    	<๋น„๊ตํŒจํ„ด 1์ด ์ผ์น˜ํ–ˆ์„ ๋•Œ ์‹คํ–‰๋˜๋Š” ๊ตฌ๋ฌธ> 	
    case <๋น„๊ตํŒจํ„ด2>, <๋น„๊ตํŒจํ„ด3> : 		
    	<๋น„๊ตํŒจํ„ด 2 ๋˜๋Š” 3์ด ์ผ์น˜ํ–ˆ์„ ๋•Œ ์‹คํ–‰๋˜๋Š” ๊ตฌ๋ฌธ> 	
    default : 		
    	<์–ด๋Š ๋น„๊ตํŒจํ„ด๊ณผ๋„ ์ผ์น˜ํ•˜์ง€ ์•Š์•˜์„ ๋•Œ ์‹คํ–‰๋˜๋Š” ๊ตฌ๋ฌธ> 
}

 

๐Ÿ’ก
Swift์—์„œ๋Š” ์ „ํ†ต์ ์ธ ์–ธ์–ด ๋ฐฉ์‹๊ณผ ๋‹ค๋ฅด๊ฒŒ ๊ฐ case ํ‚ค์›Œ๋“œ ๋ธ”๋ก๋งˆ๋‹ค break ๊ตฌ๋ฌธ์„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฆ‰, Swift์—์„œ๋Š” ์•”์‹œ์ ์ธ Fallthrough๊ฐ€ ์ ์šฉ๋˜์ง€ ์•Š์Œ.

 

fallthrough๋ฌธ&amp;amp;amp;amp;amp;amp;amp;amp;amp;nbsp;

๋•Œ๋ฌธ์˜ ๊ฐ๊ฐ์˜ case๋Š” ๋ฐ˜๋“œ์‹œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ๋ฌธ์ด ํฌํ•จ๋˜์–ด์•ผํ•จ. (๋น„์–ด์žˆ์–ด์„œ๋Š” X)

 

๐Ÿ’ก
๋ช…์‹œ์ ์œผ๋กœ fallthrough ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ, ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Œ.

fallthrough์— ์˜ํ•ด ์‹คํ–‰ํ๋ฆ„์„ ์ „๋‹ฌ๋ฐ›์€ case ๋ธ”๋ก์€ ๋น„๊ตํŒจํ„ด์˜ ์ผ์น˜์—ฌ๋ถ€์™€ ์ƒ๊ด€์—†์ด ์ž‘์„ฑ๋œ ๊ตฌ๋ฌธ์„ ์‹คํ–‰ํ•œ ํ›„ switch ๊ตฌ๋ฌธ์„ ์ข…๋ฃŒ.

 

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."

 

๐Ÿ’ก
ํ•˜๋‚˜ ์ด์ƒ์˜ ๋น„๊ตํŒจํ„ด์„ ์—ฐ์ด์–ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Œ.

ํ•˜๋‚˜์˜ case ํ‚ค์›Œ๋“œ๋กœ ๋น„๊ตํŒจํ„ด์„ ๋ฌถ์–ด ํ‘œํ˜„ํ•˜์—ฌ, ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ค ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ๋ฐ ํšจ๊ณผ์ .

 

๋‹ค์ค‘ Switch ๋ฌธ

 

๐Ÿ’ก
default ๊ตฌ๋ฌธ์„ ์ƒ๋žตํ•  ๊ฒฝ์šฐ ์™„์ „ํ•˜์ง€ ์•Š์€ ๊ตฌ๋ฌธ์œผ๋กœ ๊ฐ„์ฃผํ•˜์—ฌ ์˜ค๋ฅ˜ ๋ฐœ์ƒ

๋‹จ, ๋ชจ๋“  ํŒจํ„ด์„ ๋งค์นญ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ๊ตฌ๋ฌธ์ด ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ์— ํ•œํ•ด default๊ตฌ๋ฌธ์„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์Œ.

 

 

๐Ÿ˜Ž 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).") 
// Prints "There are dozens of moons orbiting Saturn."

 

 

 

๐Ÿค  ํŠœํ”Œ Tuple

ํŠœํ”Œ ๋‚ด๋ถ€ ์•„์ดํ…œ์ด ๋น„๊ต ๋Œ€์ƒ๊ณผ ๋ถ€๋ถ„์ ์œผ๋กœ ์ผ์น˜ํ•  ๊ฒฝ์šฐ, case ๊ตฌ๋ฌธ์˜ ๋น„๊ตํŒจํ„ด ์ „์ฒด๊ฐ€ ์ผ์น˜ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผ. ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์„ ์ƒ์ˆ˜ ๋˜๋Š” ๋ณ€์ˆ˜ํ™”ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ.

 

var value = (2, 3)  
switch value { 	
	case let (x, 3) : 		
		printf("") 	
	case let (3, y) : 		
		print("") 	
    case let (x, y) : 		
		print("") 
}

 

 

๐Ÿ˜‡ 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"

 

 

์ขŒํ‘œ

3. ์ œ์–ด์ „๋‹ฌ๋ฌธ

์ฝ”๋“œ์˜ ํ•œ ๋ถ€๋ถ„์—์„œ ๋‹ค๋ฅธ ๋ถ€๋ถ„์œผ๋กœ ์ œ์–ด ํ๋ฆ„์„ ์ „๋‹ฌํ•˜์—ฌ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜๋Š” ์ˆœ์„œ๋ฅผ ๋ณ€๊ฒฝํ•ด์ฃผ๋Š” ๊ตฌ๋ฌธ.

๐Ÿ’ก
์Šค์œ„ํ”„ํŠธ์—์„œ ์‚ฌ์šฉ๋˜๋Š” 4๊ฐ€์ง€์˜ ์ œ์–ด ์ „๋‹ฌ๋ฌธ 1. break 2. continue 3. fallthrough 4. return

 

break

๋ฐ˜๋ณต ์‹คํ–‰์ค‘์ธ ๋ฃจํ”„๋‚˜ switch ๊ตฌ๋ฌธ์—์„œ์˜ ์‹คํ–‰ํ๋ฆ„ ๋“ฑ, ์กฐ๊ฑด์‹ ๊ฒฐ๊ณผ์™€ ์ƒ๊ด€์—†์ด ์ฆ‰๊ฐ์ ์œผ๋กœ ์ข…๋ฃŒํ•˜๋Š” ๋ฐ์— ์‚ฌ์šฉ๋จ.

  • switch ๊ตฌ๋ฌธ : ๊ฐœ๋ณ„ case ๋ธ”๋ก์— ์‚ฌ์šฉ๋˜์–ด ์ „์ฒด switch ๊ตฌ๋ฌธ์˜ ์‹คํ–‰์„ ์ข…๋ฃŒํ•˜๋Š” ์—ญํ• 
  • ๋ฐ˜๋ณต๋ฌธ : ์กฐ๊ฑด์‹์ด false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์ „์— ๋ฏธ๋ฆฌ ๋ฐ˜๋ณต๋ฌธ์„ ์ข…๋ฃŒํ•˜๋Š” ์—ญํ• 
for row in 0...5 { 	
	if row > 2 { break } 	
    print("\(row) was executed.") 
}

 

continue

๊ตฌ๋ฌธ ์•„๋ž˜์— ์žˆ๋Š” ์‹คํ–‰๊ตฌ๋ฌธ์„ ๊ฑด๋„ˆ๋›ฐ๊ณ  ๋‹ค์Œ ๋ฐ˜๋ณต์„ ์‹œ์ž‘ํ•˜๋Š” ์—ญํ• .

break ๊ตฌ๋ฌธ์ด ๋ฐ˜๋ณต๋ฌธ์„ ์™„์ „ํžˆ ์ข…๋ฃŒํ•˜๋Š” ๊ฒƒ๊ณผ ๋‹ฌ๋ฆฌ continue๊ตฌ๋ฌธ์€ ๋ฐ˜๋ณต๋ฌธ์˜ ์กฐ๊ฑด์„ ๋‹ค์‹œ ํ‰๊ฐ€ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ ๋‹ค์Œ ๋ฐ˜๋ณต์„ ์‹คํ–‰.

 

for row in 0...5 { 	
	if row > 2 { continue } 	
    print("\(row) was executed.") 
}

 

๊ตฌ๋ฌธ ๋ ˆ์ด๋ธ”๊ณผ break, continue

์Šค์œ„ํ”„ํŠธ์—์„œ๋Š” ๋ฃจํ”„๋‚˜ ์กฐ๊ฑด๋ฌธ์„ ๋‹ค๋ฅธ ๋ฃจํ”„๋‚˜ ์กฐ๊ฑด๋ฌธ ์•ˆ์— ์ค‘์ฒฉ(nest)ํ•ด์„œ ์ข€ ๋” ๋ณต์žกํ•œ ํ๋ฆ„ ๊ตฌ์กฐ๋กœ ์งค ์ˆ˜ ์žˆ๋‹ค.

 

๊ทธ๋Ÿฌ๋‚˜, ๋ฐ˜๋ณต๋ฌธ๊ณผ ์กฐ๊ฑด๋ฌธ ๋ชจ๋‘ break ๊ตฌ๋ฌธ์„ ์‹คํ–‰์„ ์กฐ๊ธฐ์ข…๋ฃŒํ•˜๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

 

์ค‘์ฒฉ๋œ ๊ตฌ๋ฌธ ๋‚ด์—์„œ continue์™€ break ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด, ์‚ฌ์šฉ๋œ ๊ตฌ๋ฌธ์ด ์–ด๋–ค ๊ตฌ๋ฌธ์„ ๋ฉˆ์ถ”๊ฑฐ๋‚˜ ๋‹ค์‹œ ์‹œ์ž‘ํ•˜๊ฒŒ ํ•  ์ง€ ๋ช…ํ™•ํ•˜์ง€ ์•Š๋‹ค. ๋•Œ๋ฌธ์— ๋ช…์‹œ์ ์œผ๋กœ ๊ตฌ๋ฌธ์„ ๋ช…๊ธฐํ•˜๋ฉด ์–ด๋–ค ๊ตฌ๋ฌธ์„ break ํ•  ์ง€ ํŒ๋‹จํ•˜๊ธฐ ์ข‹์•„์ง„๋‹ค.

 

 

๐Ÿค  ๊ตฌ๋ฌธ ๋ ˆ์ด๋ธ” (Statement Label)

Swift์—์„œ๋Š” ๋ฐ˜๋ณต๋ฌธ์ด๋‚˜ ์กฐ๊ฑด๋ฌธ ๋“ฑ ํŠน์ •๊ตฌ๋ฌธ์— ๋ ˆ์ด๋ธ”์„ ๋ถ™์—ฌ ๊ธฐ์–ตํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ณ , break๋‚˜ continue ๊ตฌ๋ฌธ์ด ์‚ฌ์šฉ๋  ๋•Œ ๋ ˆ์ด๋ธ”์„ ๋ช…์‹œํ•ด์ฃผ์–ด ๊ฐœ๋ฐœ์ž๊ฐ€ ์›ํ•˜๋Š” ๊ตฌ๋ฌธ์œ„์น˜์— ์ •ํ™•ํ•œ ํ๋ฆ„์ œ์–ด๊ฐ€ ์ ์šฉ๋  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๋ฌธ๋ฒ•์„ ์ œ๊ณตํ•จ

 

 

<๋ ˆ์ด๋ธ” ์ด๋ฆ„> :
	while <์กฐ๊ฑด์‹> { <์‹คํ–‰ํ•  ๊ตฌ๋ฌธ> }  
	break <๋ ˆ์ด๋ธ” ์ด๋ฆ„> 
	continue <๋ ˆ์ด๋ธ” ์ด๋ฆ„>

 

์•„๋ž˜ ์˜ˆ์‹œ๋Š” break ๊ตฌ๋ฌธ๊ณผ continue ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜๊ณ , ๊ทธ๋ฆฌ๊ณ  ๊ตฌ๋ฌธ๋ ˆ์ด๋ธ”์„ ๊ณ๋“ค์ธ..., ๋ฑ€๊ณผ ์‚ฌ๋‹ค๋ฆฌ Snakes and Ladders ๊ฒŒ์ž„์ด๋‹ค.

์—ฌ๊ธฐ์— ํ•œ๊ฐ€์ง€ ๊ทœ์น™์„ ๋” ์ถ”๊ฐ€ํ•˜์˜€๋‹ค.

  • ์Šน๋ฆฌ ์กฐ๊ฑด : ์ •ํ™•ํžˆ 25๋ฒˆ ์‚ฌ๊ฐํ˜•์— ๋„์ฐฉํ•ด์•ผํ•œ๋‹ค.

๋งŒ์•ฝ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ฆฐ ์ˆซ์ž๊ฐ€, 25๋ฒˆ ์‚ฌ๊ฐํ˜•์„ ๋„˜์–ด๊ฐ„๋‹ค๋ฉด, 25๋ฒˆ ์ˆซ์ž์— ๋„์ฐฉํ•  ์ˆ˜ ์žˆ๋Š” ์ˆซ์ž๊ฐ€ ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ ๊ณ„์† ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ ค์•ผ๋งŒ ํ•œ๋‹ค.

 

๋ฑ€๊ณผ ์‚ฌ๋‹ค๋ฆฌ ๊ฒŒ์ž„

 

// ๋ณ€์ˆ˜, ์ƒ์ˆ˜ ์ดˆ๊ธฐํ™”  
let finalSquare = 25 
var board = [Int](repeating: 0, count: finalSquare + 1) 
board[03] = +08; board[06] = +11; board[09] = +09; 
board[10] = +02 board[14] = -10; board[19] = -11; 
board[22] = -02; board[24] = -08 var square = 0 
var diceRoll = 0

 

while ๋ฃจํ”„์™€ switch ๊ตฌ๋ฌธ์„ ์ด์šฉํ•ด ๊ฒŒ์ž„ ๋กœ์ง์„ ํ™•์žฅ. while ๋ฃจํ”„๋Š” gameLoop ๋ผ๋Š” ๊ตฌ๋ฌธ๋ ˆ์ด๋ธ”์ด ๋ถ™์Œ = ๋ฉ”์ธ ๊ฒŒ์ž„ ๋ฃจํ”„.

 

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!")

 

 

๐Ÿ’ก
๋งŒ์•ฝ break ๊ตฌ๋ฌธ์—์„œ gameLoop ๋ ˆ์ด๋ธ”์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜์œผ๋ฉด, ํ•ด๋‹น break ๊ตฌ๋ฌธ์€ while ๊ตฌ๋ฌธ์ด ์•„๋‹ˆ๋ผ switch ๊ตฌ๋ฌธ์„ ์ข…๋ฃŒ์‹œ์ผฐ์„ ๊ฒƒ. ๋ ˆ์ด๋ธ”์„ ์ด์šฉํ•˜์—ฌ, ์–ด๋–ค ์ œ์–ด๊ตฌ๋ฌธ์„ ์ข…๋ฃŒ์‹œํ‚ฌ์ง€ ๋ช…ํ™•ํ•˜๊ฒŒ ๋ช…์‹œํ•  ์ˆ˜ ์žˆ์Œ. continue ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋ฐ˜๋Œ€๋กœ ๋ ˆ์ด๋ธ”์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•œ ๋ถ€๋ถ„์€ ์•„๋‹Œ๋ฐ, ๊ทธ ์ด์œ ๋Š” ์œ„ ์ฝ”๋“œ์—์„œ๋Š” ํ•˜๋‚˜์˜ ๋ฃจํ”„๋งŒ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฌด์—‡์„ continueํ•  ์ง€๊ฐ€ ๋ช…์ง•ํ•จ. ๊ทธ๋Ÿฌ๋‚˜ ๋ ˆ์ด๋ธ”์„ ๊ฐ™์ด ์“ด๋‹ค๊ณ  ํ•ด์„œ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋ฏ€๋กœ, ์ฝ”๋“œ์˜ ๋…ผ๋ฆฌ๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ํ•˜๊ณ  ์ฝ”๋“œ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ํŽธํ•˜๊ฒŒ ํ•˜๋„๋ก break ๊ตฌ๋ฌธ๊ณผ ๊ฐ™์ด continue์—๋„ ๋ ˆ์ด๋ธ”์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ด์ฃผ๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•จ.

 

์ฐธ๊ณ 

 
https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html

 

 

    

 


๋งˆ์น˜๋ฉฐ 

์˜ค๋Š˜๋„ ์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

 

๊ถ๊ธˆํ•œ ์ ์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ, ๋„์›€์ด ๋˜์…จ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

ํ˜น์‹œ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ ํ”ผ๋“œ๋ฐฑํ•˜์‹ค  ๋‚ด์šฉ ์žˆ๋‹ค๋ฉด ์–ธ์ œ๋“  ๋Œ“๊ธ€๋กœ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

 

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

 

 

๋ฐ˜์‘ํ˜•

'๐Ÿ’ป Programming ๊ฐœ๋ฐœ > ๐ŸŽ iOS ๊ฐœ๋ฐœ, Swift' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Swift][๋ฒˆ์—ญ] ์Šค์œ„ํ”„ํŠธ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์„น์…˜ 0. ์‹œ์ž‘ํ•˜๊ธฐ ์ „์—  (1) 2022.05.13
[Swift] ์‚ฌ์šฉ์ž ์ปฌ๋Ÿฌ์…‹ ์ถ”๊ฐ€ํ•˜๊ณ  UI Color ํ™•์žฅํ•˜์—ฌ ์ฝ”๋“œ๋กœ ์ ‘๊ทผํ•˜๊ฒŒ ๋งŒ๋“ค๊ธฐ  (0) 2022.04.04
[Swift] ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•œ Cocoapods ์„ค์น˜ ๋ฐ ์„ค์ • + ์„ค์น˜๊ณผ์ •์— ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธธ ๊ฒฝ์šฐ  (0) 2022.03.28
[Swift]๋‹ค์–‘ํ•œ ๋””๋ฐ”์ด์Šค์— ๋งž์ถฐ ์…€ ์‚ฌ์ด์ฆˆ๋ฅผ ์กฐ์ •ํ•˜๊ณ ์ž ํ•  ๋•Œ + ์‚ฌ์ด์ฆˆ๊ฐ€ ์•ˆ ๋ฐ”๋€Œ์—ˆ์„ ๋•Œ ํ•ด๊ฒฐ๋ฐฉ๋ฒ• (CollectionViewDelegateFlowLayout ํ”„๋กœํ† ์ฝœ)  (0) 2022.03.17
[Swift] ๊ฐ„ํŽธํ•˜๊ฒŒ ์ •๊ทœํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ๋งค์นญํ•˜๋Š” ๋ฐฉ๋ฒ• (NSRegularExpression ์‚ฌ์šฉX)  (0) 2022.01.13

๋Œ“๊ธ€