I have a photo view app which which I use Alamofire
to get the data and use RxSwift
to observer changes in the API
. everything works well and I can print the total number of returned items and even print individual items but the app crashes when ever I set my UIlabel
text to the title value got from the code. I do not know why this is any help would be appreciated.
the crash is Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
Main cell
func configueCell(flickerPhotoModel: FlickrPhotoModel) -> Void {
self.layer.backgroundColor = UIColor.lightGray.cgColor
print("Cell \(String(describing: flickerPhotoModel.id))")
flikrTitle.text = flickerPhotoModel.title
}
PhotoViewModel
private func setupPageBinding() {
currentState.asObservable()
.subscribe(onNext: { currentState in
if currentState == .initialLoading || currentState == .loadingNextPage {
self.fetchNextPageOfPhotos()
}
}).disposed(by: disposeBag)
photos.asObservable()
.map { $0.count > 0 ? self.currentPage.value + 1 : self.currentPage.value }
.bind(to: currentPage)
.disposed(by: disposeBag)
}
COntroller
private func setupCollectionViewBinding() {
print("JSONNN x \(self.viewModel.photos.value.count)")
viewModel.photos
.asObservable()
.bind(to: collectionView.rx.items(cellIdentifier: reuseIdentifier, cellType: MainCell.self))
{ _, photo, cell in
cell.configueCell(flickerPhotoModel: photo)
print("JSONNN 5 \(self.viewModel.photos.value.count)")
}
.disposed(by: disposeBag)
viewModel.currentState
.asObservable()
.map { $0 == .initialLoading || $0 == .initialLoadingFailure }
.bind(to: collectionView.rx.isHidden)
.disposed(by: disposeBag)
collectionView.rx.modelSelected(FlickrPhotoModel.self)
.subscribe(onNext: {
self.routingDelegate?.handlePhotoSelection($0)
})
.disposed(by: disposeBag)
}