Zachary Smouse
Zachary Smouse

Reputation: 143

How to Switch an If/Else to a For Loop?

I'm doing a save button where users can pick a selected date and an array of times then save their selections. Users can pick up to three dates and array of times, then save each selection. However, is there a better way to reduce code duplication from my if/else statement? Thanks.

@objc func handleSaveDateAndTimes() {
        
    guard let selectedDay = selectedDay else { return }
        
    if selectedCardViewCount == 0 {
        selectedViews[0].view?.isHidden = false
        selectedViews[0].selectedDate = selectedDay
        viewModel.selectedDate = "\(selectedDay.month.month)-\(selectedDay.day)-\(selectedDay.month.year)"
        viewModel.selectTimesOfDay = timeChoiceArray
        daySelectionLabel.text = "1/3"
    } else if selectedCardViewCount == 1 {
        selectedViews[1].view?.isHidden = false
        selectedViews[1].selectedDate = selectedDay
        viewModel.selectedDate = "\(selectedDay.month.month)-\(selectedDay.day)-\(selectedDay.month.year)"
        viewModel.selectTimesOfDay = timeChoiceArray
        daySelectionLabel.text = "2/3"
    } else {
        selectedViews[2].view?.isHidden = false
        selectedViews[2].selectedDate = selectedDay
        viewModel.selectedDate = "\(selectedDay.month.month)-\(selectedDay.day)-\(selectedDay.month.year)"
        viewModel.selectTimesOfDay = timeChoiceArray
        daySelectionLabel.text = "3/3"
    }
        
    selectedCardViewCount += 1
    continueButton.backgroundColor = .darkPurpleTint
    continueButton.isEnabled = true
        
}

Upvotes: 0

Views: 65

Answers (2)

Ethan
Ethan

Reputation: 1373

You could simplify it without a loop like this

@objc func handleSaveDateAndTimes() {

    guard let selectedDay = selectedDay else { return }

    selectedViews[selectedCardViewCount].view?.isHidden = false
    selectedViews[selectedCardViewCount].selectedDate = selectedDay
    viewModel.selectedDate = "\(selectedDay.month.month)-\(selectedDay.day)-\(selectedDay.month.year)"
    viewModel.selectTimesOfDay = timeChoiceArray

    selectedCardViewCount += 1
    daySelectionLabel.text = "\(selectedCardViewCount )/3"

    continueButton.backgroundColor = .darkPurpleTint
    continueButton.isEnabled = true

}

Upvotes: 1

Larme
Larme

Reputation: 26066

Your if/else could be factorized like that:

let index = selectedCardViewCount > 1 ? 2 : selectedCardViewCount
let selectedView = selectedViews[index]
selectedView.view?.isHidden = false
viewModel.selectedDate = "\(selectedDay.month.month)-\(selectedDay.day)-\(selectedDay.month.year)"
viewModel.selectTimesOfDay = timeChoiceArray     
daySelectionLabel.text = "\(index+1)/3"

Some explaination:

I see that

viewModel.selectedDate = "\(selectedDay.month.month)-\(selectedDay.day)-\(selectedDay.month.year)"
viewModel.selectTimesOfDay = timeChoiceArray

Are the same each time.

Then, I see :

if selectedCardViewCount == aValue {
    selectedViews[aValue].view?.isHidden = false
    selectedViews[aValue].selectedDate = selectedDay
    daySelectionLabel.text = "aValue+1/3"
} else if selectedCardViewCount == anotherValue {
    selectedViews[anotherValue].view?.isHidden = false
    selectedViews[anotherValue].selectedDate = selectedDay
    daySelectionLabel.text = "anotherValue+1/3"
} else {
    selectedViews[2].view?.isHidden = false
    selectedViews[2].selectedDate = selectedDay
    daySelectionLabel.text = "3/3"
}

And anotherValue is aValue + 1.

So it's the selectedCardViewCount value, except that it can be superior to 2. (I don't know if it can be bigger, but I made it safe) with let index = selectedCardViewCount > 1 ? 2 : selectedCardViewCount Then I just reuse that value.

Upvotes: 2

Related Questions