Skip to main content

[WWDC19] some

Updated Markdown
Part of seriesWWDC

๐ŸŽต๊ฐ€๋”์”ฉ ๋‚˜๋„ ๋ชจ๋ฅด๊ฒŒ ์งœ์ฆ์ด๋‚˜~

๋„ˆ๋ฅผ ํ–ฅํ•œ ์ด ๋ง˜์€ ๋ณ€ํ•˜์ง€ ์•Š์•˜๋Š”๋ฐ ํ˜น์’ธ~~ ๋‚ด๊ฐ€ ์ด์ƒํ•œ๊ฑธ๊นŒ ๐ŸŽถ

๋‚ด ์ด๊ฑธ ์•„๋Š” ๋‹น์‹  ๊ทธ Some์ด ๋งž์Šต๋‹ˆ๋‹ค.

์šฐ๋ฆฌ๊ฐ€ ์—ฐ์• ํ•˜๋Š” ์ธ๋งŒํผ ๋‹ฌ๋‹ฌํ•˜์ง€ ์•Š์ง€๋งŒ ์ฝ”๋”ฉํ•  ๋• ๋‹ฌ๋‹ฌํ•œ Swift์˜ some์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.


Some SwiftUI๋ฅผ ํ•˜๋‹ค๋ณด๋ฉด ๊ต‰์žฅํžˆ ์ž์ฃผ ๋ณผ ์ˆ˜ ์žˆ์Œ

var body: some View {
    /* Any View */
}

์ด๋ ‡๊ฒŒ ๋˜์–ด์žˆ๋Š” SwiftUI View๋“ค์„!

๋ชจ๋ฅผ ๋• ๋ญ๋‹ค? ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ๋ณด์ž!

ํ•˜์ง€๋งŒ ๋”ฐ๋”ฐ๋””์”จ 2019์—์„œ ์ƒˆ๋กœ ๋‚˜์˜จ ๊ฐœ๋…์ด๊ธฐ์— ๋„์ž…๋œ ๋ฐฐ๊ฒฝ๊นŒ์ง€ ์•Œ ์ˆ˜ ์žˆ์œผ๋‹ˆ WWDC 2019๋ฅผ ๋ณด์ž!

WWDC 2019 - What's New in Swift

// Shapes Example
protocol Shape { /* ... */ }
struct Square: Shape { /* ... */ }
struct Circle: Shape { /* ... */ }
struct Oval: Shape { /* ... */ }
struct Union<A: Shape, B: Shape>: Shape { /* ... */ }
struct Transformed<S: Shape >: Shape { /* ... */ }
struct FaceShape {
    /* Something else */
    var shape: Shape { // โฌ…๏ธ Shape
        switch faceType {
        case .round:
            return Circle()
        case .square:
            return Square()
        case .diamond:
            return Transformed(Square(), by: .fortyFiveDegrees))
        default:
            return Oval()
        }
    }
}

์ž ์—ฌ๊ธฐ์„œ <= Shape๋Š” ๋ฌธ์ œ๊ฐ€ ๋ ๊นŒ?

๋ฌธ์ œ ์—†๋‹ค. ๋ฐ˜ํ™˜๋˜๋Š” ๋ชจ๋“  ๊ฐ’๋“ค์ด Shape Protocol์„ ๋”ฐ๋ฅด๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.


์ž ๊ทธ๋Ÿผ Union์„ ํ†ตํ•ด์„œ ์ƒˆ๋กœ์šด ๋„ํ˜•์„ ๋งŒ๋“ค์–ด๋ณด์ž.

๊ธฐ์กด ์‚ฌ๊ฐํ˜• + 45๋„ ๋Œ๋ฆฐ ์‚ฌ๊ฐํ˜•์„ Union ํ•˜์—ฌ 8๊ฐ์ด ์žˆ๋Š” ์Šคํƒ€ Shape๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž.

struct EightPointedStar {
    โ€ฆ
    var shape: Union<Square, Transformed<Square>> {
        return Union(Square(), Transformed(Square(), by: .fortyFiveDegrees))
    }
}

์—ฌ๊ธฐ์„œ Shape ํƒ€์ž…์ด ๋ฌธ์ œ๊ฐ€ ๋ ๊นŒ?

๋ฌธ์ œ ์—†๋‹ค. ์ฝ”๋“œ์ƒ ๋ฌธ์ œ๋Š” ์—†๋‹ค.

๋‹ค-๋งŒ ์‚ฌ์šฉ์ž์˜ ์ž…์žฅ์—์„œ๋Š” ๋ฌธ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค.

์•„ 8๊ฐœ ๊ฐ์ด ์žˆ๋Š” ์Šคํƒ€ ์–ป์–ด์•ผ์ง€!
EightPointStar ๊ตฌ์กฐ์ฒด ์„ ์–ธํ•˜๊ณ 
shape ์—ฐ์‚ฐ ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด 8๊ฐœ ๊ฐ์ด ์žˆ๋Š” ๋ณ„ ๋ชจ์–‘์˜ shape๊ฐ€ ๋‚˜์˜ค๊ฒ ์ง€!

ํ•˜์ง€๋งŒ ๊ฒฐ๊ณผ๋Š” Union ํƒ€์ž….

์‚ฌ์šฉํ•˜๋Š” ์ž…์žฅ์—์„œ๋Š” ๋˜ ๊ณต๋ถ€๋ฅผ ํ•ด์•ผํ•œ๋‹ค.

Shape์ฒ˜๋Ÿผ ์ž‘๋™ํ•˜๊ธด ํ•˜๋Š”๋ฐ Union์€ ๋˜ ๋ญ”๋ฐ??

๐Ÿ’ก
์ฆ‰ ์‚ฌ์šฉ์ž์˜ ์ž…์žฅ์—์„œ ๋ถˆํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ๋…ธ์ถœ์‹œ์ผœ์„œ API๋ฅผ ์–ด๋ ต๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค.

์—ฅ ๊ทธ๋Ÿผ ๊ทธ๋ƒฅ

struct EightPointedStar {
    โ€ฆ
    var shape: Shape {
        return Union(Square(), Transformed(Square(), by: .fortyFiveDegrees))
    }
}

์ด๋ ‡๊ฒŒ ์ฝ”๋“œ ์งœ๋ฉด ๋˜๋Š”๊ฑฐ ์•„๋‹˜?

์‘ ์•ˆ๋ผ!

์™œ??

  1. ํ”„๋กœํ† ์ฝœ ํƒ€์ž…์ด return ๋œ๋‹ค๋ฉด ๊ฐ™์€ ๊ฐ’์„ returnํ•œ๋‹ค๋Š” ๋ณด์žฅ์ด ์—†๋‹ค.

  2. ๋งŒ์•ฝ ๊ฐ™์€ API์— ๊ฐ๊ธฐ ๋‹ค๋ฅด๊ฒŒ API๋ฅผ ํ˜ธ์ถœํ–ˆ๋‹ค๋ฉด ๊ฐ™์€ ๊ฐ’์ด์—ฌ์•ผ ํ•จ -> ํ•˜์ง€๋งŒ ๊ฐ’ ์ž์ฒด๋ฅผ ๋น„๊ตํ•˜์ง€ ๋ชปํ•จ ์™œ? ํ”„๋กœํ† ์ฝœ ํƒ€์ž…์ด๋‹ˆ๊น

์งœ์ž”!๐ŸŽ‰ ๊ทธ๋ž˜์„œ ๋‚˜์˜จ ํƒ€์ž…์ด some


some

์œ„ ์˜ˆ์‹œ์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด some์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

(ํ•จ์ˆ˜)์‚ฌ์šฉ์ž ์ž…์žฅ์—์„œ ๋ถˆ๋ถ„๋ช…ํ•œ ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•˜์—ฌ ๋‚ด๋ถ€๊ตฌ์กฐ๋กœ ์–ด๋–ค ๊ฒƒ์ด ๋ฐ˜ํ™˜๋˜๋Š”์ง€ ์ •ํ™•ํ•˜๊ฒŒ ์•Œ ์ˆ˜ ์—†๋„๋ก ํ•˜๋Š” ํƒ€์ž….

์Œ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ •ํ™•ํ•˜๊ฒŒ ์•Œ ์ˆ˜ ์—†๋Š”๊ฒŒ ๊ณผ์—ฐ ์ข‹์€๊ฑธ๊นŒ? ์ƒ๊ฐํ–ˆ์Œ

๊ทผ๋ฐ ์‚ฌ์šฉํ•  ์ž…์žฅ์—์„œ๋Š” ๋•Œ๋ก  ์ •ํ™•ํ•˜์ง€ ์•Š์€๊ฒŒ ์ข‹์„ ์ˆ˜ ์žˆ์Œ

์™œ???

struct EightPointedStar {
    โ€ฆ
    var shape: Union<Square, Transformed<Square>> {
        return Union(Square(), Transformed(Square(), by: .fortyFiveDegrees))
    }
}

์œ„ ์˜ˆ์‹œ ์ฒ˜๋Ÿผ some์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ €๋ ‡๊ฒŒ Union์„ ๋ฐ˜ํ™˜์‹œ์ผœ๋„ ๋จ

ํ•˜์ง€๋งŒ ์ € Square๊ฐ€ ์•„๋‹Œ Circle์ด ๋“ค์–ด๊ฐ„๋‹ค๋ฉด? ์ €๊ฑธ ๋‹ค ํ•˜๋‚˜ ํ•˜๋‚˜ ๋‹ค ์„ ์–ธํ•ด์ค˜์•ผํ•จ

struct EightPointedStar {
    โ€ฆ
    var shape: Union<Circle, Transformed<Square>> {
        return Union(Circle(), Transformed(Circle(), by: .fortyFiveDegrees))
    }
}

์ด๋ ‡๊ฒŒ ์ด๊ฑด ๋„ˆ๋ฌด ๋ถˆ-ํŽธํ•ด

๋‚œ ๊ทธ์ € Shape๊ฐ€ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋ฟ์ธ๋ฐ...

๊ทธ๋ž˜์„œ ๋ถˆ๋ถ„๋ช…ํ•œ๊ฒŒ ์ข‹์„ ์ˆ˜ ์žˆ๋Š” ๊ฑฐ์ž„

struct EightPointedStar {
    โ€ฆ
    var shape: some Shape {
        return Union(Square(), Transformed(Square(), by: .fortyFiveDegrees))
    }
}

์ด๋Ÿฌ๋ฉด ์•„๋ฌด๊ฒƒ๋„ ๋ฌป์ง€๋„ ์•Š๊ณ  ๋”ฐ์ง€์ง€๋„ ์•Š๊ณ  Shape ์‚ฌ์šฉ ๊ฐ€๋Šฅ

ํƒ€์ธ์ด '๋‚˜๋ฅผ ์–ด๋–ป๊ฒŒ ํ‰๊ฐ€ํ•˜๋Š”๊ฐ€'๋Š” ์ƒ๊ฐ๋ณด๋‹ค ์ค‘์š”ํ•˜๋‹ค โ€“ ์ชผ๋ฐ•์˜ ๋ฐ”์ด์˜คํ…Œํฌ ์Šคํƒ€ํŠธ์—… ์ด์•ผ๊ธฐ

About the author

  • ์•ˆ๋…•ํ•˜์„ธ์š”๐Ÿ™‡๐Ÿปโ€โ™‚๏ธ ์„ธ์ƒ์„ ๋” ํŽธ๋ฆฌํ•˜๊ฒŒ ๋ฐ”๊พธ๊ณ  ์‹ถ์€ iOS ๊ฐœ๋ฐœ์ž ์ตœ์ธํ˜ธ์ž…๋‹ˆ๋‹ค.

    Hello ๐Ÿ‘‹ I'm Inho Choi, an iOS developer who wants to change the world more conveniently.

    • ๋Œ€ํ•™๊ต ์กธ์—…
    • Apple Developer Academy @ POSTECH 1๊ธฐ
    • KWDC Main Organizer
    • AsyncSwift Organizer

No comments yet. Be the first to comment.

More from this blog