[ad_1]
I try to add new item in array who already fill from an API, but i got an error when my item finish displaying in my onboarding. this is my error
“*** __boundsFail: index 3 beyond bounds [0 .. 2]”
i know the problem is, but i still can’t fix it. The problem is since i mixed a hardcoded data with the data from an API i populate the data wrongly.
this is my viewModel
enum FilterKey {
static let regular = "regular"
static let hajj = "multicasa_hajj"
}
class OnboardingViewModel {
var thematicItems: [ThematicItem] = []
func getThematicItem() {
getMulticasaConfigUseCase.getThematicItem()
.subscribe(onSuccess: { [weak self] thematicItems in
guard let strongSelf = self else { return }
strongSelf.thematicItems = strongSelf.addNewThematicItems(items: thematicItems)
}).disposed(by: disposedBag)
}
private func addNewThematicItems(items: [ThematicItem]) -> [ThematicItem] {
var thematicItems = items
thematicItems.append(ThematicItem(key: "regular", imgURL: "some https image url.png", title: "label_multicasa_onboarding_page_title_0".localized, description: "label_multicasa_onboarding_page_desc_0".localized, content: nil, visible: false, order: 1))
thematicItems.append(ThematicItem(key: "regular", imgURL: "some https image url.png", title: "label_multicasa_onboarding_page_title_1".localized, description: "label_multicasa_onboarding_page_desc_1".localized, content: nil, visible: false, order: 2))
thematicItems.append(ThematicItem(key: "regular", imgURL: "some https image url.png", title: "label_multicasa_onboarding_page_title_2".localized, description: "label_multicasa_onboarding_page_desc_2".localized, content: nil, visible: false, order: 3))
return thematicItems
}
}
This is my OnboardingViewController in viewDidLoad
class OnboardingViewController {
viewModel.getThematicItem()
let thematicItems = viewModel.thematicItems
switch onboardingType {
case .regular:
let regularOnboarding = thematicItems.filter { $0.key == FilterKey.regular }
pagingProgressView.pageSize = regularOnboarding.count
regularOnboarding.forEach {
let onboardingView: OnboardingPageView = .fromNib()
onboardingView.setup(thematicItem: $0)
stackView.addArrangedSubview(onboardingView)
onboardingView.snp.makeConstraints { make in
make.size.equalTo(scrollView.snp.size)
}
}
case .haji:
let hajjOnboarding = thematicItems.filter { $0.key == FilterKey.hajj }
pagingProgressView.pageSize = hajjOnboarding.count
hajiOnboarding.forEach {
let onboardingHajiView: OnboardingAlaHajj = .fromNib()
onboardingHajjView.setup(thematicItem: $0)
stackView.addArrangedSubview(onboardingHajjView)
onboardingHajiView.snp.makeConstraints { make in
make.size.equalTo(scrollView.snp.size)
}
}
}
}
How the correct way to populate the data so is not crashing?
[ad_2]