본문 바로가기

Programming/Swift

(21)
Key path를 활용한 Swift Closure 활용하기 키 경로를 사용하여 코드를 더 읽기 쉽고 효율적으로 만드는 방법을 소개하도록 하겠습니다. 다음과 같이 회사의 이름 및 public 여부 등을 나타내는 Company struct가 있다고 해보겠습니다. struct Company { let name: String let isPublic: Bool let stockPrice: Int } let companies: [Company] = [ Company(name: "Apple", isPublic: true, stockPrice: 140), Company(name: "Facebook", isPublic: true, stockPrice: 12), Company(name: "Google", isPublic: false, stockPrice: 333), Company..
[WWDC2021] Your guide to keyboard layout 애플이 지난 WWDC2021에 이를 개선한 keyboardLayoutGuide를 발표했습니다. 그동안 키보드가 보여질때 나머지 View영역이 가려지는 걸 방지하기 위해 keyboardWillShowNotification등의 observer등을 통해 키보드가 올라올때, 또는 키보드가 내려갈 때 키보드 높이를 계산해 나머지 View Layout을 업데이트 해줘야하는 번거로움이 있었습니다. 이번에 새롭게 등장한게 바로 UIKeyboardLayoutGuide이라는 녀석입니다. 사용방법은 아래와 같습니다. (매우 심플해졌군요...) 키보드 높이에 영향받는 View가 있다면 keyboardLayoutGuide의 topAnchor을 통해 AutoLayout을 지정하면 됩니다. 아래 샘플은 버튼을 KeyboardLay..
withCheckedContinuation memory leak 현상 및 해결방법 Handler를 async/await으로 바꾸려는 경우 핸들러에 withCheckedContinuation를 추가해 아래와 같이 사용하는데요 Class MessageManager { func recentMessages() async -> [String] { return await withCheckedContinuation { continuation in self.requestMessages() { list in continuration.resume(returning: list) } } } } let messages = await recentMessages() 평상시에는 문제가 되지 않으나 만약 Class가 부득이하게 종료해야하는 상황이 오면, 기존에 요청했던 async를 모두 완료 되기 전에는 정상적으로..
[SwiftUI] @Published 사용법 및 @ObservedObject와의 관계 SwiftUI의 View 및 ViewModel 관계에서 주로 쓰는게 @Published와 @ObservedObject인데요 Class FooView { @ObservedObject viewModel = FooViewModel() } Class FooViewModel: ObservableObject { @Publihsed foo: String } 이들 관계 및 사용 관련해서 이해하기 좋은 블로그를 하나 소개하려 합니다. https://pilgwon.github.io/post/published-risks-and-usage-explained [수위프트UI/번역] 코드로 알아보는 @Published의 사용법과 위험성 @Published risks and usage explained with code exampl..
[SwiftUI] VStack과 LazyVStack과의 차이 VStack과 LazyVStack 또는 HStack과 LazyHStack 기본 요지는 - VStack은 전체 데이터를 메모리에 담아두고 스크롤 할때마다 보여줌 - LazyVStack은 데이터가 화면에 렌더링 되는 순간 보여지는 순간 그려짐 둘 차이만 봐도 메모리 차이가 심할 것 같아요 (미리 화면을 다 그려놓고 보여주느냐, 렌더링 되는 순간에 그려주느냐) 그럼 많이들 쓰는List는 어떤방식이냐? 궁금했는데요 https://developer.apple.com/forums/thread/651256 를 살펴보니 "List contents are always loaded lazily" List는 모두 Lazy하게 그려준다고 하니, 스크롤 형식의 많은 내용을 보여줄때는 List를 쓰면 될 것 같습니다. 참고사이트..
[SwiftUI] @FocusState Property Wrapper iOS 15에 추가된 FocusState Property Wrapper입니다. Focus를 자유롭게 이동시키기 위한 Property Wrapper이며 아래와 같이 선언해서 사용합니다. @FocusState var isFocusField: Bool @State var email: String UITextField("email", text: $email, prompt: Text("email")) .focus($isFocusField) 만약 포커스할 입력창 여러개 있어야 할 경우, 이렇게 각 필드마다 FocusState를 줘야하는데요 @FocusState var isFocusEmail: Bool @State var email: String @FocusState var isFocusPassword: Bool @..
realm swift 강의 실무에서 활용하는 프로토콜 중심 프로그래밍 프로토콜 지향 MVVM을 소개합니다.
swift 구조체에서 protocol 사용 예제 1 struct에서 공통된 기능을 쓰기 위해서 protocol을 통해 추상화 과정을 거친다. 하지만 일부 기능은 구현이 필요 없는 경우도 존재한다. protocol Car { func stop() { } func autostop() { } } struct Truck: Car { func stop() { } func autostop() { } } struct Van: Car { func stop() { } func autostop() { } } 일부 추상화 함수를 extension을 통해 작성하게 되면, 해당 프로토콜을 채택한 struct에서 모두 구현해야할 필요가 없어지게 된다. protocol Car { func stop() { } } extension Car { func autostop() { } } s..

반응형