๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun
article thumbnail

์ดˆ๋ฐ˜์€  ๊ธฐ๋ณธ์ ์œผ๋กœ ์•„๋Š” ์ง€์‹ ์€ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ •๋ฆฌํ•˜๊ฑฐ๋‚˜ ํŒจ์Šคํ•˜๊ณ   ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ ๋œ ์ง€์‹  ์œ„์ฃผ๋กœ ์ •๋ฆฌํ•ด ๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค.

for๋ฌธ์˜ stride ๋ฉ”์†Œ๋“œ

for i in stride(from: 0, to: 10, by: 2) {
    print(i) // 0, 2, 4, 6, 8
}

for i in stride(from: 0, through: 10, by: 2) {
    print(i) // 0, 2, 4, 6, 8, 10
}

// reverseํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์“ธ ์ˆ˜ ์žˆ๋‹ค.
for i in stride(from: 3, through: 0, by: -1) {
    print(i) // 3, 2, 1, 0
}
  • 1์”ฉ ์ฆ๊ฐ€๊ฐ€ ์•„๋‹Œ ์›ํ•˜๋Š” ์ˆซ์ž๋งŒํผ ์ฆ๊ฐ€ ์‹œํ‚ค๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ
  • to : toํŒŒ๋ผ๋ฏธํ„ฐ์— ์žˆ๋Š” ์ˆ˜๋ฅผ ์ œ์™ธํ•œ๋‹ค.
  • through: throughํŒŒ๋ผ๋ฏธํ„ฐ์— ์žˆ๋Š” ์ˆ˜ ํฌํ•จํ•œ๋‹ค.

 

repeat while ๊ตฌ๋ฌธ

var num = 1
repeat {
    num += 1
} while num <= 5

print(num) // 6

๋ฃจํ”„์˜ ์กฐ๊ฑด์„ ํŒ๋‹จํ•˜๊ธฐ ์ „์— ๋ฃจํ”„ ๋ธ”๋Ÿญ์„ ์ฒ˜์Œ์— ํ•œ ๋ฒˆ ๋จผ์ € ํ†ต๊ณผํ•œ๋‹ค.

 

switch ๊ตฌ๋ฌธ

  • break ๊ตฌ๋ฌธ ์š”์ฒญ ์—†์ด ์ฒ˜์Œ ์ผ์น˜ํ•˜๋Š” switch ์ผ€์ด์Šค๊ฐ€ ์™„๋ฃŒ๋˜์ž๋งˆ์ž switch ๊ตฌ๋ฌธ ์ „์ฒด๊ฐ€ ๋๋‚œ๋‹ค.
  • ํ•ญ์ƒ ์™„๋ฒฝํ•ด์•ผ ํ•œ๋‹ค. → ๋ชจ๋“  ์ผ€์ด์Šค๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•จ
    • ๋”ฐ๋ผ์„œ default ํ‚ค์›Œ๋“œ๋กœ ๊ธฐ๋ณธ ์ผ€์ด์Šค๋ฅผ ์ •์˜ํ•œ๋‹ค.
    enum Color {
        case red
        case blue
    }
    
    let myColor: Color = .blue
    
    switch myColor {
    case .red:
        print("red")
    case .blue:
        print("blue")
    }
    
    • ๋ชจ๋“  ์ผ€์ด์Šค๊ฐ€ ๊ณ ๋ ค๋๋‹ค๋ฉด default ํ‚ค์›Œ๋“œ ํ•„์š”์—†์Œ (ex enumํƒ€์ž…์˜ ๋ชจ๋“  ์ผ€์ด์Šค ๊ณ ๋ คํ•œ ๊ฒฝ์šฐ)

 

let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a", "A":
    print("The letter A")
default:
    print("Not the letter A")
}
  • ๋‘ ๊ฐ’์„ ์ฝค๋งˆ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ผ€์ด์Šค ๊ฒฐํ•ฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

let anotherPoint = (2, 2)
switch anotherPoint {
case (let x, let y):
    print("on the x-axis with an x value of \\(x) \\(y)")
    fallthrough
case (0, _):
    print("on the y-axis with a x value of 0")
case let (x, y) where x == y:
    print("(\\(x), \\(y)) is on the line x == y")

}

// on the x-axis with an x value of 2
// on the y-axis with a x value of 0
  • switch ๊ตฌ๋ฌธ์— ์—ฌ๋Ÿฌ ๊ฐ’์ธ ํŠœํ”Œ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • _ ๋ฌธ์ž๋กœ ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ๊ฐ’ ๋ฐ”์ธ๋”ฉ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. (ex) let x)
  • fallthrough ํ‚ค์›Œ๋“œ๋กœ ๋‹ค์Œ ์ผ€์ด์Šค๋กœ ๋„˜์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.

 

๋ผ๋ฒจ์ด ์žˆ๋Š” ๊ตฌ๋ฌธ

whileLoop: while true {
    for i in 0..<10 {
        if i == 9 {
            break whileLoop
        }
    }
}
  • while๋ฌธ์— ๋ผ๋ฒจ์„ ๋ถ™์—ฌ for๋ฌธ ์•ˆ์˜ break ๋กœ ๋ฐ”๋กœ ํƒˆ์ถœ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

Defer

if score < 10 {
    defer {
        print(score)
    }
    defer {
        print("The score is:")
    }
		defer {
        print("Wow")
    }
    score += 5
}
// Wow
// The score is:
// 6
  • if ๊ตฌ๋ฌธ์˜ ๋ณธ๋ฌธ์ด ์ข…๋ฃŒ๋˜๊ธฐ ์ „์— ์‹คํ–‰๋œ๋‹ค.
  • ํ”„๋กœ๊ทธ๋žจ์ด ์–ด๋–ป๊ฒŒ ์ข…๋ฃŒํ•˜๋Š”์ง€์— ๊ด€๊ณ„์—†์ด defer ์•ˆ์˜ ์ฝ”๋“œ๋Š” ํ•ญ์ƒ ์ˆ˜ํ–‰๋œ๋‹ค.
  • defer ๋ธ”๋Ÿญ์ด ์—ฌ๋Ÿฌ๊ฐœ๋ฉด ์ฒซ๋ฒˆ์งธ defer ๋ธ”๋Ÿญ์€ ๋งˆ์ง€๋ง‰์— ์ˆ˜ํ–‰๋œ๋‹ค.
profile

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun

@iosun

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!

profile on loading

Loading...