본문 바로가기

전체 글65

Stack 구현하기 자료구조(Data Structure)를 공부하고, 복습하기 위해 앞으로 정리를 하겠습니다..!! 그 첫번째로는 Stack!! FILO(First In Last Out) 혹은 LIFO(Last In First Out)을 만족하는 데이터 저장하는 방법이라고 생각하면 됩니다! Stack의 구현 요소로는, 1. 현재 stack안의 데이터 갯수 2. 현재 stack이 비었는지를 파악할 수 있게 해주는 Bool값 3. stack에 값을 추가 할 수 있는 push() 함수 4. stack에서 값을 꺼내는 pop() 함수 정도가 됩니다! Stack 구현을 해볼까요? struct Stack { var stack: [T] = [] var count: Int { return stack.count } var isEmpty: .. 2023. 1. 10.
DataSource말고 DiffableDataSource로 DiffableDataSource DiffableDataSource의 정의 먼저 봐보자(나는 UITableView를 예시로 삼아 글을 작성할꺼다!, UICollectionView도 거의 동일) @preconcurrency @MainActor class UITableViewDiffableDataSource : NSObject where SectionIdentifierType : Hashable, SectionIdentifierType : Sendable, ItemIdentifierType : Hashable, ItemIdentifierType : Sendable 내 블로그에서 조금 다뤄봤던 @MainActor로 선언되어 있고, DataSource를 구성하는 값은 2개를 받는데, 한개는 SectionIden.. 2023. 1. 6.
protocol 이럴때 사용해보세요 Swift는 프로토콜 지향언어다 라는 이야기가 있듯이 Swift는 프로토콜의 활용이 굉장히 중요하다. 그럼 프로토콜은 언제 쓰는데요?! 를 위해서 정리해 본다. 먼저 가벼운 뷰를 만들고 상황을 보자! struct DefaultColorTheme { let primary: Color = .blue let secondary: Color = .white let tertiary: Color = .gray } struct Protocols: View { let colorTheme: DefaultColorTheme = DefaultColorTheme() var body: some View { ZStack { colorTheme.tertiary .ignoresSafeArea() Text("Protocol") .fon.. 2022. 12. 27.
AsyncPublisher (구독을 하지 않고 데이터의 변화를 확인) AsyncPublisher란, 관찰하고 싶은 객체에 대한 구독을 하지 않고 그 value들을 확인 할 수 있다. class AsyncPublisherDataManager { @Published var myData: [String] = [] // 2초 마다 myData에 데이터를 넣어주는 함수 func addData() async { myData.append("Apple") try? await Task.sleep(nanoseconds: 2_000_000_000) myData.append("Banana") try? await Task.sleep(nanoseconds: 2_000_000_000) myData.append("Orange") try? await Task.sleep(nanoseconds: 2_000.. 2022. 12. 26.