Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- Value Type Reference Type
- @escaping
- tableview section별 다른 cell적용
- 테이블뷰 나누기
- NavigationSearchBar
- TableView Section
- 강한 참조 순환
- Input Output
- til
- SWIFT
- retain cycle
- 자료구조
- 양궁대회
- TableView
- 면접을 위한 CS 전공 지식 노트 Tree
- 면접을 위한 CS전공 지식 노트
- UIKit
- wil
- Carousel CollectionView
- CarouselCollectionview
- 프로그래머스
- class struct
- ReferceCycle
- coremotion
- CoreData
- Reference Cycle
- Array vs Linked List
- firebase
- 롤케이크 자르기
- UserDefaults
Archives
- Today
- Total
개발하는 동글 :]
[TIL],[UIKit],[Observables 패턴 사용해보기!] 본문
1. Observables 패턴 사용해 보기!
MVVM패턴을 사용하기 위해서 Observable 패턴 사용을 연습해 보았다
2. Observable 클래스를 생성
class Observable<T> {
var value: T {
didSet {
listener?(value)
}
}
private var listener: ((T) -> Void)?
init(_ value: T) {
self.value = value
}
func bind(_ closure: @escaping (T) -> Void) {
closure(value)
listener = closure
}
}
Observable 타입을 이용하여 bind 및 value값을 사용가능한 상태로 만들어준다.
3. Observable Type을 채택한다.
final class FTOPViewModel {
var rangeBTtitle: Observable<String> = Observable("단어장 선택")
var count: Observable<Int> = Observable(10)
}
viewModel의 두 프로퍼티는 Observable 타입을 이용하여 bind 및 value값을 사용가능한 상태가 되었다.
4. bind 부분을 정의
viewModel.vocaList.bind { [weak self] list in
guard let self = self else {return}
if list.isEmpty {
viewModel.rangeBTtitle.value = "단어장 선택"
} else {
viewModel.rangeBTtitle.value = "총 \(viewModel.vocaList.value.count)개의 단어장"
}
}
그럼 rangeBTtitle의 bind 부분을 정의해 주자
즉 이 bind 메서드를 통해 viewModel의 vocaList가 didset 즉 값이 변할 때 뷰를 새롭게 그려주게 된다
이러한 경험으로 Observable 패턴을 이용하여 값이 변할 때 그에 반응하는 코드를 구성할 수 있게 되었다.
5. TIL
5.1 오늘의 회고
1. 팀 프로젝트 과제
2. Observable 사용하기
5.2 내일의 목표
1. 팀 프로젝트 발표 마무리 하기