set label text with array value / for loop

We are new to Xcode and are having a problem setting text labels with array values. The following works fine; the print command is only included to ensure that the array is working. All is good except we'd like to set the values using a for loop and array.


        English0.text = animal0.animalName
        Norwegian0.text = animal0.norwegianName
        image0.image = UIImage(named: "animal\(animal0.animalNumber)")
        print(animals[0])


Next we tried to use the array to set the English label, but the result was a crash due to line 01:

        English0.text = animals[0].animalName
        Norwegian0.text = animal0.norwegianName
        image0.image = UIImage(named: "animal\(animal0.animalNumber)")
        print(animals[0])


If we can get the labels to be set with the array values, then we'd like to proceed with a loop to set the 10 animals.


Any help you can provide is appreciated.

Thanks,

Mike

Accepted Reply

What is the error message from the crash?


I don't like having the init() in the struct do the addition to the array. I think it would be much better style to remove the "animals.append(self)" from the struct initializer, and instead of "let animal0 = ..." use "animals.append(...)"


Having the series of labels in stack views is fine. Adding them to outlet collections (one for English name labels, one for Norwegian name labels, each with 10 labels) is a separate step. You'll need an array of them anyway, so you might as well let IB create it for you. Otherwise you'd have to make all 20 outlets into your code individually, and create the array in code yourself.

Replies

This is not a question about a feature of the Xcode IDE, so it's posted in the wrong forum. Getting Started would be better.


How is "animals" declared and initialized? What was the exact error message from the crash?


If you are expecting to set 10 of each text field in a for loop, then they will also have to be in an array. Assuming your UI is done in a storyboard / XIB, an outlet collection would be perfect for this. When you create an outlet collection, you'll see that its type in the code is an array of UITextField, so you can iterate through that the same way you would iterate through an array of String (I assume that's what you have your names in).

Thanks,

Per your suggestion I have moved this to the "Getting Started" Forum.


animals is an array with members declared and initialized in Games.swift This is done in a Struct as follows:


struct animal {
    let animalNumber: Int
    let animalName: String
    let norwegianName: String
    
    var fileName: String {
        return "animal\(animalNumber)"
    }
    
    init(animalNumber: Int, animalName: String, norwegianName: String) {
        self.animalNumber = animalNumber
        self.animalName = animalName
        self.norwegianName = norwegianName
        animals.append(self)
        
    }
}
var animals = [animal]()

let animal0 = animal(animalNumber: 0, animalName: "chicken", norwegianName: "Høne")
let animal1 = animal(animalNumber: 1, animalName: "rooster", norwegianName: "Hane")
let animal2 = animal(animalNumber: 2, animalName: "Deer", norwegianName: "Hjort")
let animal3 = animal(animalNumber: 3, animalName: "Llama", norwegianName: "llama")
let animal4 = animal(animalNumber: 4, animalName: "Cow", norwegianName: "Ku")
let animal5 = animal(animalNumber: 5, animalName: "Donkey", norwegianName: "Esel")
let animal6 = animal(animalNumber: 6, animalName: "Sheep", norwegianName: "Sau")
let animal7 = animal(animalNumber: 7, animalName: "Goat", norwegianName: "Geit")
let animal8 = animal(animalNumber: 8, animalName: "Cat", norwegianName: "Katt")
let animal9 = animal(animalNumber: 9, animalName: "Dog", norwegianName: "Hund")


Unfortunately we did not use an outlet collection and are using a series of labels (all contained in stacks).


The loop and array worked perfectly fine in the playground, but we can't get animals.animalName to populate the labels.


Hope you can help,

Mike

What is the error message from the crash?


I don't like having the init() in the struct do the addition to the array. I think it would be much better style to remove the "animals.append(self)" from the struct initializer, and instead of "let animal0 = ..." use "animals.append(...)"


Having the series of labels in stack views is fine. Adding them to outlet collections (one for English name labels, one for Norwegian name labels, each with 10 labels) is a separate step. You'll need an array of them anyway, so you might as well let IB create it for you. Otherwise you'd have to make all 20 outlets into your code individually, and create the array in code yourself.

Many thanks for this. You were right about the method of appending to the array.


I didn't realize that IB can also create an array....so I'll have to look that up and learn how to do it.


Thanks again,

Mike