0

I have 4 different progressView

@IBOutlet weak var firstProgressBar: UIProgressView!
@IBOutlet weak var secondProgressBar: UIProgressView!
@IBOutlet weak var thirdProgressBar: UIProgressView!
@IBOutlet weak var fourthProgressBar: UIProgressView!

And I have an array with 4 values

[30,20,24,25]

Since this array change from the API call (I need to check if I really have this value), what is the best and safer way to:

  • Order this array from bigger to smaller
  • Iterate throught this array and set each of my progressView value to the corresponding array value

This is my current code:

func setupProgressBar(item: CustomObject) {
   let array = item.array // [x,y,z,w] array 

   self.progressBar.setProgress( ?? , animated: false)
}

I have an animation for the value but it's not the point of my question, i just need to set values

3
  • Which progress bar should get which array value? Also, what do you mean by the "best way"? Can you define that? Commented Jul 3, 2020 at 11:54
  • First progress bar => First array value ; Second progress bar => Second array value and so on Commented Jul 3, 2020 at 11:55
  • With "Best way" I mean without setting one by one, and by doing it in a safe way (no crash cuz I don't have value for example) Commented Jul 3, 2020 at 11:56

2 Answers 2

3

A better approach would be to connect all the UIProgressBar in your storyboard to an @IBOutlet collection.

@IBOutlet weak var progressBars: [UIProgressView]!

Then iterate through the array to set the value of progressBars.

func setupProgressBar(item: CustomObject) {
    let array = item.array // [x,y,z,w] array
    for (progressBar, value) in zip(progressBars, array) {
        progressBar.setProgress(value, animated: false)
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

That's interesting, I didnt think about this! Could you add some specific code for my example? Let's say I have to add this 4 array values to this collection of ProgressBar
I've already added the modified setupProgressBar method as an example.
@Frankenstein question: what will happen if the count of progressBars and item.array are not equal? Will it crash?
That's a good point, I can try this out if @Frankenstein couldnt' help us! Btw thank you very much, I learned 2 new stuff!
@MubashirAli It doesn't crash for that scenario. It's Safer!
2
func setupProgressBar(item: CustomObject) {
    let valueArray = item.array.sorted  // [x,y,z,w] array
    let progressBars = [firstProgressBar, second….., fourthProgressBar]
    for item in valueArray.enumerated() {
        progressBars[item.index].setProgress(item.value)
    }
}

Make sure that progress bars and values are equal. Otherwise the code will crash.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.