[WWDC19] some
์๋ ํ์ธ์๐๐ปโโ๏ธ ์ธ์์ ๋ ํธ๋ฆฌํ๊ฒ ๋ฐ๊พธ๊ณ ์ถ์ 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
๐ต๊ฐ๋์ฉ ๋๋ ๋ชจ๋ฅด๊ฒ ์ง์ฆ์ด๋~
๋๋ฅผ ํฅํ ์ด ๋ง์ ๋ณํ์ง ์์๋๋ฐ ํน์ธ~~ ๋ด๊ฐ ์ด์ํ๊ฑธ๊น ๐ถ
๋ด ์ด๊ฑธ ์๋ ๋น์ ๊ทธ 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์ ๋ ๋ญ๋ฐ??
์ฅ ๊ทธ๋ผ ๊ทธ๋ฅ
struct EightPointedStar {
โฆ
var shape: Shape {
return Union(Square(), Transformed(Square(), by: .fortyFiveDegrees))
}
}
์ด๋ ๊ฒ ์ฝ๋ ์ง๋ฉด ๋๋๊ฑฐ ์๋?
์ ์๋ผ!
์??
ํ๋กํ ์ฝ ํ์ ์ด return ๋๋ค๋ฉด ๊ฐ์ ๊ฐ์ returnํ๋ค๋ ๋ณด์ฅ์ด ์๋ค.
๋ง์ฝ ๊ฐ์ 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 ์ฌ์ฉ ๊ฐ๋ฅ
