본문 바로가기

100daysofswiftui

(3)
SwiftUI] StateObject, Published, ObservableObject https://www.hackingwithswift.com/books/ios-swiftui/sharing-swiftui-state-with-stateobject swiftui 에서 struct 인스턴스 변수는 간단한 @State 로 (내부 프로퍼티의) 변경을 감지하여 UI에 반영할 수 있다. 하지만 class 인스턴스 변수는 @State 를 붙여도 내부 프로퍼티의 변경을 감지하지 못한다. class 의 내부 프로퍼티의 변경을 감지하도록 하는 방법은 다음과 같다. 1. 외부에서 변경을 감지하려는 클래스의 프로퍼티에 @Published 를 붙여준다. class User { @Published var name: String = "Micky" @Published var age: Int = 10 } 2. view..
[SwiftUI] @State, struct, class https://www.hackingwithswift.com/books/ios-swiftui/why-state-only-works-with-structs why state only works with structs 라는 제목의 강의. swiftui 에서 변경될 수 있는 변수에 @State 를 붙이는 것에 익숙해졌다. 이 @State 변수에는 스위프트의 기본 형 뿐만 아니라, 사용자형도 사용할 수 있다. 다음과 같다. struct User { var firstName = "Bilbo" var lastName = "Baggins" } struct ContentView: View { @State private var user = User() var body: some View { VStack { VStack {..
100DaysOfSwiftUI - Day 24 마지막 과제가 아래와 같다. 커스텀 ViewModifier (와 View extension) 을 만드는데, 글씨체를 크고 푸른색으로 만들어 프로미넌트타이틀에 적당하게 바꾸는 모디파이어. Create a custom ViewModifier (and accompanying View extension) that makes a view have a large, blue font suitable for prominent titles in a view. 작성한 코드는 아래와 같다. import SwiftUI struct ProminentTitle : ViewModifier { func body(content: Content) -> some View { content .font(.largeTitle.bold()) ...