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 |