๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป/Swift

9. ๋ฐ˜๋ณต๋ฌธ - iOS ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์œ„ํ•œ ์Šค์œ„ํ”„ํŠธ ๊ธฐ์ดˆ

reujusong 2020. 6. 17. 16:57

 

  • for-in
  • while
  • repeat-while

 

 - for in ๊ตฌ๋ฌธ

  • ๊ธฐ์กด ์–ธ์–ด์˜ for-each ๊ตฌ๋ฌธ๊ณผ ์œ ์‚ฌํ•˜๋‹ค.
  • Dictionary์˜ ๊ฒฝ์šฐ ์ดํ„ฐ๋ ˆ์ด์…˜ ์•„์ดํ…œ์œผ๋กœ ํŠœํ”Œ์ด ๋“ค์–ด์˜จ๋‹ค.

 

var integers = [1, 2, 3]
let people = ["kdgt": 10, "eric": 15, "mike": 12]

for integer in integers {
    print(integer)
}

// Dictionary์˜ item์€ key์™€ value๋กœ ๊ตฌ์„ฑ๋œ ํŠœํ”Œ ํƒ€์ž…์ด๋‹ค
for (name, age) in people {
    print("\(name): \(age)")
}

 

 

 - while ๊ตฌ๋ฌธ

 

while integers.count > 1 {
    integers.removeLast()
}

 

 

 - repeat-while ๊ตฌ๋ฌธ

  • ๊ธฐ์กด ์–ธ์–ด์˜ do-while ๊ตฌ๋ฌธ๊ณผ ํ˜•ํƒœ/๋™์ž‘์ด ์œ ์‚ฌํ•˜๋‹ค.
  • do๋ฅผ ์“ฐ์ง€ ์•Š๋Š” ์ด์œ ๋Š” ์˜ˆ์™ธ์ฒ˜๋ฆฌ์—์„œ do๊ฐ€ ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

repeat {
    integers.removeLast()
} while integers.count > 0