074_MVVM+DiffableDataSource(Unsplash: Get a random photo)
2022. 10. 22. 22:21ㆍSeSAC/과제
#. 체크리스트
1. Codable사용할 때 옵셔널 타입 체크하기: Observable.value를 nil로 초기화 하려면 옵셔널 타입이어야 함.
2. snapshot.appendItems()에 클로저값 넣을 때 viewDidLoad보다 Codable이 먼저 실행(서치바검색어가 전달되지 않은 상황)됨. 결국 스냅샷에 nil을 넣는 상황이 되기 때문에 nil이 아닌 경우에 bind메서드 실행하도록 분기 처리 해줘야함.
1.
struct RandomPhoto: Codable, Hashable {
var description: String?
var urls: randomPhotoUrls?
}
// MARK: - randomPhotoUrls
struct randomPhotoUrls: Codable, Hashable {
let raw, full, regular, small, thumb: String
}
//코더블 RandomPhoto타입을 초기화
var randomPhoto: CObservable<RandomPhoto> = CObservable(RandomPhoto(description: "", urls: nil))
2.
func bindData() {
viewModel.randomPhoto.bind { photo in
var snapshot = NSDiffableDataSourceSnapshot<Int, randomPhotoUrls>()
snapshot.appendSections([0])
guard let data = photo.urls else { return }
snapshot.appendItems([data])
print(photo.urls)
self.dataSource.apply(snapshot)
}
'SeSAC > 과제' 카테고리의 다른 글
2차 평가과제_Memo App Project (0) | 2022.10.30 |
---|---|
018_환율 변동 프로그램 (0) | 2022.07.29 |
014_화면전환(present, push, pull) (0) | 2022.07.22 |
012-2 테이블뷰(해리포터 영화정보 화면) (0) | 2022.07.21 |
012-1 테이블뷰(쇼핑리스트) (0) | 2022.07.20 |