Getting ".text" to labels using loops and arrays?

  1. I’ve got 20 labels.
  2. They’ll get assigned values to display via a loop in a function
  3. I’m trying not to write 20 full statements of LABEL1.TEXT = “blah blah”, LABEL2.TEXT = “blah”, and so on.

I’ve got 20 labels here:

var round1 : UILabel!
var round2 : UILabel!
(and so on)

I’ve made a string array of all the label names:

ex: labelNameArray = [“round1”, “round2”, “round3”…“round20”]

I have an array of all the values to be shown on the labels.

ex: valuesForLabels = [100, 200, 300, … 2000]

Is there a way to “swiftly” do this:==> round1.text = valuesForLabels[0]

by doing something like this?:===> labelNameArray[0].text = valuesForLabels[0]

That way I can run the iteration in a loop and avoid writing a function with 20 full statements giving value to label.text.

I’ve tried making an array of UILabels but I can’t get them populated with anything, and I keep running across "cannot assign value of String to subscript type UILabel. So, it begs the question of what CAN you pass into an array of UILabels?

I know I’m circling the solution—syntactically I’m missing something.

You would use an IBOutletCollection for this.

See this Sean Allen video for more info.

Easy.

Firstly, your labelNameArray needs to have the label names without quotes so that it is actually an array of UILabel.

so you would declare it as:

var labelNameArray: [UILabel] = [round1, round2, round3]
for index in 0...labelNameArray.count - 1 {
    labelNameArray[index].text = String(valuesForLabels[index])
}

This is a sample of code I used in Playgound to check:

let round1 = UILabel()
let round2 = UILabel()
let round3 = UILabel()

var labelNameArray: [UILabel] = [round1, round2, round3]
var valuesForLabels = [100, 200, 300]

for index in 0...labelNameArray.count - 1 {
    labelNameArray[index].text = String(valuesForLabels[index])
}


print("\(round1.text!)")
print("\(round2.text!)")
print("\(round3.text!)")



and the output in the console was:

100
200
300

Thanks for both posts. I like his videos.

That still has me enter the UILabel array by hand.
I know, by the time I fix this I can enter all 20 by hand and be done. But, I’m really curious to learn if there is a way to do this:

var labelNameArray: [UILabel] = [ ]

for i in 0…19 {

let name = “round” + String(i) ( i.e., round1, round2, round3…round20)

labelNameArray.append(name)

}

this looks great—works less than that. :slight_smile:

I get this: Cannot convert value of type ‘String’ to expected argument type ‘UILabel’

What is the argument type that goes in an array of type UILabel? Aren’t they just names?

Yes, you’ll need to create the labels in Storyboard and connect them to your VC. Then your array of label names will work.

yeah, I just literally deleted the screen shot as I was figuring that out. Thanks. I’m picking up this much speed now.

If you were to create each label in code you would have a whole lot of constraints to create as well and if you are not familiar with creating constraints in code then avoid it.

My second VC is connected modally. It may not be the swiftest way to do that, but I didn’t have time to work with adjusting the navigation toolbar. And, it’s just a static page that slides up, shows some data, and gets swiped down.

  1. I haven’t connected the variables from the first VC yet; I’m using local values to make sure it works (filling the labels)

  2. the app runs, but when I press the button to slide VC 2 up into view I get this crash message in the appDeleage swift file.

I don’t have a key round133. I have a round13; or is “round” a system vocabulary word that is coincidental to my naming?

You would use an IBOutletCollection for this.

See this Sean Allen video for more info.

@roosterboy

the collection view seems to make changes apply to all the labels and not individually as needed.

An IBOutletCollection is an array of related UIKit controls. You can access them individually just like any other array and change their properties.

So, for instance, this code:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var labels: [UILabel]!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        for idx in 0..<labels.count {
            labels[idx].text = "Label Index: \(idx)"
        }
        
    }

}

produces this result:

ok, I’ll give it a try. thank you

So, if I understand correctly, I drag a label to my viewcontroller.swift and choose outlet collection.

Then, I drag my other labels to it as well.

If I hover over that line of code in the .swift it highlights all the labels.

Question: How do you know which label as at which index? Your code looks good, but are they indexed by the order in which I added them to the collection?

Yes, that is correct.

yep. as I added them I hovered and saw the indexes. Thanks so much. Two enthusiastic thumbs up on this site.

I hope to contribute in a better way soon.