How can I add make an array for all of my IBOutlets?

These are the @IBOutlets I need out in an array

@IBOutlet weak var progressBar1: UIProgressView!
@IBOutlet weak var progressBar2: UIProgressView!
@IBOutlet weak var progressBar3: UIProgressView!
@IBOutlet weak var progressBar4: UIProgressView!

Solution 1:

Open the Assistant Editor, right-click and drag from one of your UIProgressView's or just drag from its "Referencing Outlet Collections" to the code file. Insert outlet collection

Then you can drag from your swift file's @IBOutlet to the rest of your UIProgressView's. Add view to collection

Solution 2:

class ViewController: UIViewController {
    @IBOutlet weak var p1: UIProgressView!
    @IBOutlet weak var p2: UIProgressView!
    @IBOutlet weak var p3: UIProgressView!
    @IBOutlet weak var p4: UIProgressView!
    
    var outlets: [UIProgressView] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        outlets = [
            p1,p2,p3,p4
        ]
    }
}

If you have other types of views you can use

var outlets: [UIView] = [...]

Solution 3:

On top declare a variable first like this

var outlets: [UIProgressView] = []

and now on ViewDidLoad method you can use this to put all outlets on that array

like this:

outlets = [progressBar1, progressBar2, progressBar3, progressBar4]

Hope you understand.

Solution 4:

As mentioned here Swift - IBOutletCollection equivalent you can use IBOutletCollection to do that. You can drag all your views to one IBOutlet array.

@IBOutlet weak var progressBars: [UIProgressView]!

For example, you can access the first progressBar like

progressBars[0]

But you have to careful about the order of progressBars, when you define IBOutletCollections the collection will not be order guaranteed. You can define the for each view and sort by their tags in runtime as mentioned here also Is IBOutletCollection guaranteed to be of correct order?

To order all views by their tags like

progressBars = progressBars.sorted { $0.tag < $1.tag }