2022. 6. 16. 18:05ใProgramming/Swift
[ ๊นจ์ ๊ถ๊ธ์ฆ๋ค ๐ง ]
1) ์ปจํ ์ธ ๋ทฐ: ํ๋ฉด์ ๊ทธ๋ฆฌ๋ ๊ตฌ์กฐ์ฒด
2) ํ๋ฆฌ๋ทฐ : ํ๋ฆฌ๋ทฐ๋ฅผ ๊ทธ๋ฆฌ๋ ๊ตฌ์กฐ์ฒด
- text ๋ฐ์ .์ ํตํ์ฌ ์์ฑ๋ค์ ๋์ดํ๋ ๊ฒ
: modifier ๋ผ๊ณ ๋ถ๋ฅธ๋ค!
ex) .font, .fontWeight, .padding ๋ฑ๋ฑ
[ some ์ ๋ฌด์์ธ๊ฐ ? ]
[ ์ถ์ฒ: https://babbab2.tistory.com/158 ]
some์ด๋ผ๋ ํค์๋๋ Swift 5.1์์ ๋ฑ์ฅํ ์๋ก์ด ๊ธฐ๋ฅ์ผ๋ก,
ํด๋น ํค์๋๊ฐ ๋ฐํ ํ์ ์์ ๋ถ์ ๊ฒฝ์ฐ,
ํด๋น ๋ฐํ ํ์ ์ด ๋ถํฌ๋ช ํ ํ์ (Opaque Type)!
: ๋ถํฌ๋ช ํ ํ์ ์ด๋ -> "์ญ ์ ๋ค๋ฆญ ํ์ (reverse generic types)
์์์ ์ ๋ค๋ฆญ์ด ํจ์ "์ธ๋ถ"์์ ํด๋น ํ์ ์ ๋ํด ์ ์ ์๋ ๋ฐ๋ฉด,
๋ถํฌ๋ช ํ ํ์ ์ ๊ฒฝ์ฐ, ์ธ๋ถ์์ ํจ์์ ๋ฐํ ๊ฐ ์ ํ์ ์ ํํ๊ฒ ์ ์ ์์
๋ค๋ง ํจ์ ๋ด๋ถ์์๋ ์ด๋ค ํ์ ์ ๋ค๋ฃจ๋์ง ์ ํํ ์๊ณ ์์
(๊ทธ๋ฌ๊ฒ, ์ ํํ ๋ฐ๋๋ค..?)
๋ช ํํ์ง ์์ ํ์ (associatedType or Self)์ด ํ๋กํ ์ฝ ๋ด์ ์ ์๋์ด ์๊ณ ,
์ด ํ๋กํ ์ฝ์ ํจ์(๋ฐ ์ฐ์ฐ ํ๋กํผํฐ)์ ๋ฐํ ํ์ ์ผ๋ก ๊ฐ์ง ๋
๋ฐํ ํ์ ์ "๋ถํฌ๋ช ํ์ "์ผ๋ก ๋ง๋ค์ด์ฃผ๊ธฐ ์ํด ์ฌ์ฉํ๊ณ ,
: ๋ฐํ ๊ฐ ์ ํ์ ์ ํํ๊ฒ ์ ์ ์๋ค!
some์ด๋ผ๋ ๊ฒ์ ํตํด ๋ฐํ ํ์ ์ "๋ถํฌ๋ช ํ์ "์ผ๋ก ๋ง๋ ๋จ ๊ฒ์,
๋ฐํ ํ์ ์ด ์ด๋ค ํ์ ์ธ์ง ์ปดํ์ผ๋ฌ(๋ฐ ํจ์ ์ธ๋ถ)๋ 1๋ ๋ชจ๋ฅด๊ฒ ์ง๋ง
ํจ์ ๋ด๋ถ์์ ์ด๋ค ํ์ ์ ๋ฐํํ๋์ง ๋ช ํํ ์๊ณ ์๊ณ , (๋ด๋ถ๋ ์๊ณ ์๋ค!)
๋ฐ๋ผ์ ๋ด ํจ์๋ ์ ํด์ง "ํน์ ํ์ ๋ง ๋ฐํ"๋๋ค๊ณ ์ปดํ์ผ๋ฌ์๊ฒ ์๋ ค์ฃผ๋ ๊ฒ์
// ํ.. ์ด๋ ต๋ค.. ๋จธ๋ฆฌ ํฐ์ง๋ค. .์ผ ใ ์
์ ์์ ์์ "ํน์ ํ์ "์ ํจ์ ๋ด๋ถ์์ ์ด๋ป๊ฒ ๊ตฌํํ๋์ ๋ฐ๋ผ ๋ฌ๋ผ์ง๊ฒ ์ง๋ง,
GiftBox ํ๋กํ ์ฝ์ ์ค์ํ๊ณ ์๋ AppleGiftBox / CherryGiftBix ์ค ํ๋๊ฐ ๋ ๊ฒ์
[ some ์ ์ฌ์ฉํ ๋์ ์ด์ ! ]
์ฌ๊ธฐ์ body๋ผ๋ ๊ฒ์ View๋ผ๋ ํ๋กํ ์ฝ์ ์ ์๋ ํ๋กํผํฐ์ธ๋ฐ,
์ด ํ๋กํผํฐ๋์ฐ์ฐ ํ๋กํผํฐ(Computed Property)๋ก
์ ์ฝ๋์์ Text๋ผ๋ ๊ฒ์ return ํ๊ณ ์๋ค๊ณ ๋ณด๋ฉด ๋จ
์, ๊ทผ๋ฐ ์์์ ๋ฐฐ์ด ๊ฒ์ ํ ๋๋ก ํ๋ค๋ฉด
some์ ์ธ์ ์ด๋ค??
๋ช ํํ์ง ์์ ํ์ (associatedType or Self)์ด ํ๋กํ ์ฝ ๋ด์ ์ ์๋์ด ์๊ณ ,
์ด ํ๋กํ ์ฝ์ ๋ฐํ ํ์ ์ผ๋ก ๊ฐ์ง๊ณ ์ถ์ ๋ ์ด๋ค!!!
์คํ ๊ทธ๋ผ View๋ ํ๋กํ ์ฝ์ผ ๊ฒ์ด๊ณ !!! ์์ฑ ์ค ํ๋๋ associatedtype(or Self)๋ก ์ ์ธ๋์ด ์๊ฒ ๋ค!?
// body๊ฐ associatedtype ์ผ๋ก view (ํ๋กํ ์ฝ) ๋ด๋ถ์ ์ ์๋์ด ์์
์ด๋ฅผ ๋ฐํ ํ์ ์ผ๋ก ๊ฐ๊ณ ์ถ์ ๋
๋ช ํํ์ง ์์ ํ์ (associatedType or Self)์ด ํ๋กํ ์ฝ ๋ด์ ์ ์๋์ด ์๊ณ ,
์ด ํ๋กํ ์ฝ์ ๋ฐํ ํ์ ์ผ๋ก ๊ฐ์ง๊ณ ์ถ์ ๋ some์ ์ด๋ค! :)
public protocol View {
/// The type of view representing the body of this view.
///
/// When you create a custom view, Swift infers this type from your
/// implementation of the required ``View/body-swift.property`` property.
associatedtype Body : View
/// The content and behavior of the view.
///
/// When you implement a custom view, you must implement a computed
/// `body` property to provide the content for your view. Return a view
/// that's composed of built-in views that SwiftUI provides, plus other
/// composite views that you've already defined:
///
/// struct MyView: View {
/// var body: some View {
/// Text("Hello, World!")
/// }
/// }
///
/// For more information about composing views and a view hierarchy,
/// see <doc:Declaring-a-Custom-View>.
@ViewBuilder var body: Self.Body { get }
}
[์ ๋ฆฌ]
๊ทธ๋ผ body๋ผ๋ ์ฐ์ฐ ํ๋กํผํฐ๋ View๋ผ๋ ํ๋กํ ์ฝ ํ์ ์ ๋ฐํํ์ง๋ง
View๋ผ๋ ํ๋กํ ์ฝ ๋ด์ ๋ช ํํ์ง ์์ ํ์ (body)์ด ์ ์๋์ด ์์ผ๋
some์ ํตํด ๋ถํฌ๋ช ํ์ ์ด๋ผ๊ณ ๋ฐํ์ค ๊ฒ์ด๊ตฌ๋!
๋ฐ๋ผ์ body ๋ด๋ถ์ ๋ด๊ฐ ์์ฑํ๋ ์ฝ๋์ ๋ฐ๋ผ ๋ฆฌํด ํ์ ์ด ๋ฌ๋ผ์ง๊ฒ ์ง๋ง,
View ํ๋กํ ์ฝ์ ์ค์ํ๋ ํ์ ๋ง ๋ฆฌํด์ด ๊ฐ๋ฅํ๊ฒ ๊ตฐ!
( if text -> button )
์ปดํ์ผ๋ฌ(๋ฐ ํจ์ ์ธ๋ถ)์์๋ View๋ผ๋ ํ๋กํ ์ฝ์ ์ค์ํ๋ ๊ฐ์ฒด๊ฐ ๋์ฌ ๊ฒ์ ์์ง๋ง,
์ ํํ ์ด๋ค ํ์ ์ด ๋์ฌ์ง๋ ๋ชจ๋ฆ
๋ค๋ง ์์์ body๋ ํน์ ํ์ (ํ์ฌ๋ ๋ด๊ฐ Text๋ฅผ ๋ฆฌํด ํ์ผ๋ Text)๋ง
ํญ์ ๋ฐํ๋๋จ ๊ฒ์ ์๋ ค์ฃผ๋ ๊ฒ์ด some(๋ถํฌ๋ช ํ์ )์
์ฅ ๊ฐ์๊ธฐ ๊ธฐํ์๊ฐ ๋ด๋ถ ๋์์ธ์ Text๊ฐ ์๋ ๋ฒํผ์ผ๋ก ๋ฐ๊ฟ๋ฌ๋ผ๋ค?
์ด๋ ๊ฒ text๋ฅผ Button์ผ๋ก ๋ฐ๊พธ๋ฉด ์ปดํ์ผ๋ฌ ๋ฐ ํจ์ ์ธ๋ถ์์
์ฌ์ ํ ์ ํํ ์ด๋ค ํ์ ์ด ๋์ฌ์ง๋ ๋ชจ๋ฆ
๋ค๋ง ์์์ body๋ ํน์ ํ์ (ํ์ฌ๋ ๋ด๊ฐ Button๋ฅผ ๋ฆฌํด ํ์ผ๋ Button)๋ง
ํญ์ ๋ฐํ๋๋จ ๊ฒ์ ์๋ ค์ฃผ๋ ๊ฒ์ด some(๋ถํฌ๋ช ํ์ )์
[ summary ]
์ด๋ ๊ฒ ๋ถํฌ๋ช ํ ํ์ (some)์ ์ฌ์ฉํ ๊ฒฝ์ฐ,
ํจ์ ๋ด๋ถ์์ ๋ด๊ฐ ์ง๋ ์ฝ๋์ ๋ฐ๋ผ ์์๊ฐ๊ฐ ๋ฆฌํด ํ์ ์ด ๋ณ๊ฒฝ ๋์์ง๋ง (Text -> Button)
์ด๊ฑฐ์ ๋ํด ๋ฐ๋ก ๋ด๊ฐ ๋ฆฌํด ํ์ ์ ๋ฐ๊ฟ์ค ํ์๊ฐ ์์!!!
๋ง์ฝ some์ ํตํด ๋ถํฌ๋ช ํ์ ์ผ๋ก ์ ์ธํ์ง ์์๋ค๋ฉด
๋งค๋ฒ body์ ํ์ ์ ์ ๋ ๊ฒ ๋ค ๋์ฐํ๊ฒ ๋ช ๋ช ํด์คฌ์ด์ผ ํ์ ๊ฑฐ์
ํ๋ง๋๋ก ๋ถํ์ํ ํ์ ์ ๋ํด ์ฐ๋ฆฌ๊ฐ ์ ํ์๊ฐ ์์! ์ปดํ์ผ๋ฌ๊ฐ ์์์ ํจ
-> ์ฝ๊ฒ ui ์์๋ค์ ๊ตฌ์ฑํ๊ธฐ ์ํด, ์ฌ์ฉ์ฑ์ ๋์ด๋ ์ญํ ์ ํ๋ ๋ฏ ํ๋ค.
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
// previews : PreviewProvider ํ๋กํ ์ฝ์ ํ์ ๊ตฌํ ์ฌํญ
// previews ํ์
ํ๋กํผํฐ์์ ๋ทฐ ์์ฑ
ContentView()
// ์๋ ๊ฒฐ๊ตญ ํ ๋ทฐ ์์ฑ์ผ๋ก ์ด์ด์ง๊ฒ ์ง?
}
}
#endif
public protocol PreviewProvider : _PreviewProvider {
/// The type to preview.
///
/// When you create a preview, Swift infers this type from your
/// implementation of the required
/// ``PreviewProvider/previews-swift.type.property`` property.
associatedtype Previews : View
/// A collection of views to preview.
///
/// Implement a computed `previews` property to indicate the content to
/// preview. Xcode generates a preview for each view that you list. You
/// can apply ``View`` modifiers to the views, like you do
/// when creating a custom view. For a preview, you can also use
/// various preview-specific modifiers that customize the preview.
/// For example, you can choose a specific device for the preview
/// by adding the ``View/previewDevice(_:)`` modifier:
///
/// struct CircleImage_Previews: PreviewProvider {
/// static var previews: some View {
/// CircleImage()
/// .previewDevice(PreviewDevice(rawValue: "iPad Pro (11-inch)"))
/// }
/// }
///
/// For the full list of preview-specific modifiers,
/// see <doc:Previews-in-Xcode>.
@ViewBuilder static var previews: Self.Previews { get }
/// The platform on which to run the provider.
///
/// Xcode infers the platform for a preview based on the currently
/// selected target. If you have a multiplatform target and want to
/// suggest a particular target for a preview, implement the
/// `platform` computed property to provide a hint,
/// and specify one of the ``PreviewPlatform`` values:
///
/// struct CircleImage_Previews: PreviewProvider {
/// static var previews: some View {
/// CircleImage()
/// }
///
/// static var platform: PreviewPlatform? {
/// PreviewPlatform.tvOS
/// }
/// }
///
/// Xcode ignores this value unless you have a multiplatform target.
static var platform: PreviewPlatform? { get }
}
[ content view ์ดํดํ๊ธฐ ]
[ ์ถ์ฒ: https://babbab2.tistory.com/159?category=829015 ]
1) body๋ ๋จ ํ๊ฐ์ View๋ง ๋ฐํ !
: ์ฌ๋ฌ๊ฐ์ text๋ฅผ ๋ฐฐ์นํ๊ณ ์ถ์ ๋, ๊ผญ stack์ผ๋ก ๋ฌถ์ด์ค์ผ ํจ
: ์ฆ, ์ฌ๋ฌ ๊ฐ์ ๋ทฐ๋ค์ ํ๋์ ๋ทฐ๋ก ๊ฐ์ธ์ค์ ํ๋์ View๋ก ๋ฆฌํดํด์ผ ํจ!!!
- ์ค์ ์ ์ฝ๋์์ body์ ํ์ ์ VStack<TupleView<Text,Text>>์ผ๋ก
body๋ VStack์ด๋ผ๋ ํ๋์ View๋ก ๋ฐํ๋๋ ๊ฒ์!
- ๋ค๋ง ์ฐ๋ฆฐ ์์ ๋ณต์กํ ํ์ ์ ๋ํด ์ ํ์ ์๊ณ ๋ช ์ํ ํ์๋ ์๋ค!
-> body๊ฐ some View ์ฆ, ๋ถํฌ๋ช ํ์ ์ ๋ฆฌํดํ๋๊น! ํํ
2) ContentView์ ๋ณ์๋ ์์๋ฅผ ์ถ๊ฐ ?
-> body ์์๋ค๊ฐ ํด์ฃผ๋ฉด ๋๋ค!
struct ContentView : View {
var name: "Colli"
var body: some View {
Text("Hello")
}
}
3) View์ ์์ ์ฃผ๊ธฐ๋ฅผ ๊ด๋ฆฌํ๊ณ ์ถ๋ค๋ฉด ?
๊ทธ.. UIKit์ผ๋ก ๊ฐ๋ฐํ ๋ ์ฌ์ฉํ๋ View Controller์ ์์ ์ฃผ๊ธฐ ์์์?
ViewDidAppear
ViewDidDisappear
...
๋ฑ๋ฑ.. ์ด๋ฐ ๊ฑฐ ์ด๋ป๊ฒ ์ฌ์ฉํ๋๋ฉด
onAppear
onDisappear
๋ผ๋ modifier๋ฅผ ์ด์ฉํ๋ฉด ๋จ!! :)
struct ContentView : View {
var body: some View {
Text("Hello")
.onAppear {
print("ํ
์คํธ๊ฐ ๋ณด์
๋๋ค")
}
}
}
๋ฑ๋ฑ ์ด๋ฐ ์์ผ๋ก!
+
+ ์ฐธ๊ณ ๋ก
์์ body ํ๋กํผํฐ๋ View๊ฐ ์์ฑ๋๊ณ ํ ๋ฒ๋ง ๋ง๋ค์ด์ง๋ ๊ฒ ์๋๋ผ
View์ Life Cycle ๋์ ํ์ํ ๋๋ง๋ค ์ฌ๋ฌ ๋ฒ์ฉ ์๋ก ๋ง๋ค์ด์ ธ์ ๋ฆฌํด๋จ
๊ทธ๋ผ ์ฑ๋ฅ์ ๋ฌธ์ ์๋ ๊ฑฐ ์๋๊ฐ์? ์ถ๊ฒ ์ง๋ง,
SwiftUI์์ View ์์ฒด๊ฐ ํด๋์ค๊ฐ ์๋ ๊ตฌ์กฐ์ฒด๋ก ๋งค์ฐ๋งค์ฐ ๊ฐ๋ฒผ์ ok
'Programming > Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ Fileprivate ๋? (0) | 2022.07.21 |
---|---|
๐ฉโ๐ป UUID(Universally Unique IDentifier)๋? (0) | 2022.06.30 |
๐ARC (Automatic Reference Counting) : ์๋ ์ฐธ์กฐ ๊ณ์ฐ (0) | 2022.06.24 |
@IBAction & @IBOutlet ๐ค (0) | 2022.06.24 |
[SwiftUI] Geometry Reader (0) | 2022.06.16 |