ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Swift] if case let ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถˆํ•„์š”ํ•œ ์ฝ”๋“œ ์ค„์ด๊ธฐ
    iOS/๐ŸŸ  Swift 2022. 1. 16. 21:53

    ์•ˆ๋…•ํ•˜์„ธ์š”๐Ÿถ

    ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” if case let ํŒจํ„ด์„ ์‚ฌ์šฉํ•ด switch-case ๊ตฌ๋ฌธ์˜ boiler plate ๋ฅผ ์ค„์—ฌ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

     

    ์–ธ์ œ ์“ฐ๋Š”๊ฐ€?

    ์—ด๊ฑฐํ˜•์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ๋ณดํ†ต ์ €๋Š” switch-case ๋ฅผ ์‚ฌ์šฉํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.

    ๋‹จ์ผ case ๋งŒ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ์‹ถ์–ด๋„ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

    ๋–ก๋ณถ์ด ์—ด๊ฑฐํ˜•์„ ์˜ˆ๋กœ ํ•œ ๋ฒˆ ๋“ค์–ด๋ณด์ž๋ฉด:

    enum ๋–ก๋ณถ์ด {
      case ์ผ๋ฐ˜
      case ์น˜์ฆˆ
    }
    
    let ๋‚ด๋–ก๋ณถ์ด = ๋–ก๋ณถ์ด.์น˜์ฆˆ
    
    switch ๋‚ด๋–ก๋ณถ์ด {
      case .์น˜์ฆˆ:
      	์น˜์ฆˆํ† ํ•‘์–น๊ธฐ()
      case .์ผ๋ฐ˜:
      	break;
    }

     

    ๋งŒ์•ฝ ๋” ๋งŽ์€ ์ผ€์ด์Šค๊ฐ€ ์žˆ๋Š”๋ฐ ํ•˜๋‚˜๋งŒ ์ฒ˜๋ฆฌํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ๋ผ๋ฉด, default ๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

    switch ๋‚ด๋–ก๋ณถ์ด {
      case .์น˜์ฆˆ:
      	์น˜์ฆˆํ† ํ•‘์–น๊ธฐ()
      default:
      	break
    }

     

    ์ด๋ ‡๊ฒŒ ๋‹จ ํ•˜๋‚˜์˜ case ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด switch ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด,

    ๋‚˜๋จธ์ง€ case ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด break ๋“ฑ์˜ ์˜๋ฏธ์—†๋Š” ์ฝ”๋“œ๋ฅผ ์ ์–ด์ฃผ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜ญ

     

    ๊ทธ๋Ÿฐ๋ฐ ๋” ์ข‹์€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

    if let case ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด

    ์•„๋ž˜์™€ ๊ฐ™์ด ์šฐ๋ฆฌ๋Š” ๋” ๊ฐ„๊ฒฐํ•œ ์ฝ”๋“œ๋กœ [case ์น˜์ฆˆ] ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

    if case let .์น˜์ฆˆ = ๋‚ด๋–ก๋ณถ์ด {
    	์น˜์ฆˆํ† ํ•‘์–น๊ธฐ()
    }

     

    ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”๊ฐ€?

     

    if case let .์น˜์ฆˆ = ๋‚ด๋–ก๋ณถ์ด {
    	์น˜์ฆˆํ† ํ•‘์–น๊ธฐ()
    }

     

    ๋ฌธ๋ฒ•์ด ์กฐ๊ธˆ ํŠน์ดํ•ฉ๋‹ˆ๋‹ค.

    '๋‚ด๋–ก๋ณถ์ด' ๊ฐ€ ํ• ๋‹น ์—ฐ์‚ฐ์ž(=) ์˜ค๋ฅธ์ชฝ์— ์œ„์น˜ํ•˜๊ณ  ์žˆ๊ณ ,

    ์ฒ˜๋ฆฌํ•  case ๊ฐ€ ์™ผ์ชฝ์— ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค.

    ์œ„ ์ฝ”๋“œ์—์„œ '๋‚ด๋–ก๋ณถ์ด' ๋ผ๋Š” ๋–ก๋ณถ์ด ์—ด๊ฑฐํ˜• ์ธ์Šคํ„ด์Šค๊ฐ€ [case ์น˜์ฆˆ] ๋ผ๋ฉด, if ๋ธ”๋Ÿญ์€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

     

    ๊ทธ๋Ÿฐ๋ฐ ์œ„์™€ ๊ฐ™์€ ๊ฒฝ์šฐ let ๋„ ์‚ฌ์‹ค ์ ์–ด์ค„ ํ•„์š”๊ฐ€ ์—†๋‹ต๋‹ˆ๋‹ค :)

    ๋”ฐ๋ผ์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ ์–ด์ฃผ์–ด๋„ ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ๋ฒ•์ž…๋‹ˆ๋‹ค.

    // O.K
    if case .์น˜์ฆˆ = ๋‚ด๋–ก๋ณถ์ด {
    	์น˜์ฆˆํ† ํ•‘์–น๊ธฐ()
    }

     

    ๊ทธ๋Ÿผ let ์€ ์–ธ์ œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ผ๊นŒ์š”?

    let ์€ ์—ฐ๊ด€๊ฐ’์„ ๋ฐ”์ธ๋”ฉ ํ• ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

    ์—ฐ๊ด€๊ฐ’ ๋ฐ”์ธ๋”ฉ์— ์ต์ˆ™ํ•˜์‹œ๋‹ค๋ฉด ์•„๋ž˜ ๋‘ ์ข…๋ฅ˜์˜ ์ฝ”๋“œ๊ฐ€ ๊ธˆ๋ฐฉ ์ดํ•ด๋˜์‹ค ๊ฒƒ์ž…๋‹ˆ๋‹ค.

     

    ์ด๋ฅผ ์œ„ํ•ด ๋–ก๋ณถ์ด ์—ด๊ฑฐํ˜•์— [case ๋ฏน์Šค] ๋ฅผ ์ถ”๊ฐ€ํ•ด๋ณผ๊ฒŒ์š”!

    enum ๋–ก๋ณถ์ด {
      ...
      case ๋ฏน์Šค(๋–ก๋น„์œจ: Double, ์˜ค๋Ž…๋น„์œจ: Double)
      ...
    }

    [case ๋ฏน์Šค] ๋Š” ๋–ก๋ณถ์ด๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋–ก๊ณผ ์˜ค๋Ž…์˜ ๊ตฌ์„ฑ๋น„์œจ์„ Double ํ˜• ์—ฐ๊ด€๊ฐ’์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

     

    ๋งŒ์•ฝ if-case-let ์„ ์‚ฌ์šฉํ•˜์—ฌ ์—ฐ๊ด€ ๊ฐ’์„ ์ฒ˜๋ฆฌํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”์ธ๋”ฉ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    let ๋‚˜๋งŒ์˜๋–ก๋ณถ์ด์กฐํ•ฉ = ๋–ก๋ณถ์ด.๋ฏน์Šค(๋–ก๋น„์œจ: 0.64, ์˜ค๋Ž…๋น„์œจ: 0.36)
    
    if case let .๋ฏน์Šค(๋–ก์–‘, ์˜ค๋Ž…์–‘) = ๋‚˜๋งŒ์˜๋–ก๋ณถ์ด์กฐํ•ฉ {
        ๋–ก๋„ฃ๊ธฐ(๋–ก์–‘)
        ์˜ค๋Ž…๋„ฃ๊ธฐ(์˜ค๋Ž…์–‘)
    }

     

    ์ด๊ฒƒ์€ ์—ฐ๊ด€๊ฐ’ ๋ฐ”์ธ๋”ฉ ๋ฐฉ์‹๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    ๋งŒ์•ฝ ๋–ก์–‘์—๋Š” ๊ด€์‹ฌ์ด ์—†๋‹ค๋ฉด _ (์–ธ๋”์Šค์ฝ”์–ด)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์™€์ผ๋“œ์นด๋“œ ํŒจํ„ด์œผ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

    if case .๋ฏน์Šค(_, let ์˜ค๋Ž…๋น„์œจ) = ๋‚˜๋งŒ์˜๋–ก๋ณถ์ด์กฐํ•ฉ {
        ๋–ก๋„ฃ๊ธฐ(1 - ์˜ค๋Ž…๋น„์œจ)
        ์˜ค๋Ž…๋„ฃ๊ธฐ(์˜ค๋Ž…๋น„์œจ)
    }

     

    ์ถ”๊ฐ€์ ์œผ๋กœ if ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ guard ๋ฌธ์—๋„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ, ์กฐ๊ฑด๋ฌธ ๋’ค์— ์—ฐ๊ฒฐ๋˜๋Š” ํ‘œํ˜„์‹์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    // guard-case-let 
    guard case .์น˜์ฆˆ = ๋‚ด๋–ก๋ณถ์ด else {
        print("์•„ ์™œ ์น˜์ฆˆ๋กœ ์•ˆ์‚ฌ์˜ค๋ƒ๊ณ !")
        return
    }
    
    
    // chained expressions
    guard let ์ฃผ๋ฌธํ•œ๋–ก๋ณถ์ด = ๋ฐฐ๋ฏผ์ฃผ๋ฌธ(), case .์น˜์ฆˆ = ์ฃผ๋ฌธํ•œ๋–ก๋ณถ์ด else {
    	print("์•„ ์ž˜๋ชป์‹œ์ผฐ๋„ค;;;")
    	return 
    }

     

     


    ์˜ค๋Š˜๋„ ๋‚˜๋Š” ์„ฑ์žฅํ–ˆ๋‹ค!!๐Ÿ”ฅ

     

    ๐Ÿค–๐ŸŸข[์šฐ์งฑ์˜ iOS ๋ธ”๋กœ๊ทธ]๐Ÿ”ต๐Ÿ’ป

    iOS๋ฅผ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ๋ฐฐ์šด ๋‚ด์šฉ์„ ๊ธฐ๋กํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

     

    [์ฐธ๊ณ  ๋งํฌ]

    https://goshdarnifcaseletsyntax.com

     

    How Do I Write If Case Let in Swift?

    if case let is an abbreviated form of switch case let immediately precedes the candidate pattern in both versions. Confusingly though, when using if case let, the value comes after the = operator. So if case let Puppy.mastiff(droolRating, weight) = fido {

    goshdarnifcaseletsyntax.com

    ๋Œ“๊ธ€

์–ด์ œ๋ณด๋‹ค ๋ฐœ์ „ํ•œ ๋‚˜