'Equivalent to a JS array of objects in Swift. My current implementation throws an error 'property initializers run before self'. Is this possible?

I want my users to loop through an interface where they see a picture and can press a button to generate a sound that belongs to the picture.

To do that, I am trying to build a data structure in Swift that holds two key-value pairs so that I can call the value of each property at the current index of wherever my user happens to be.

One of the properties is an image with a UIImage value and the other is a sound with a string value. After trying several things, the best I could come up with is the below implementation but that throws the following error at each of the variables in my animals variable. Error: Cannot use instance member 'bird' within property initializer; property initializers run before 'self' is available.

When I declare the animals variable with local scope, i.e. inside a function, it works fine. But I need that variable to have global scope. I've been searching quite a bit and have to admit I don't completely understand the error that's been thrown.

Can anyone point out what I am doing wrong?

Code in XCode:

class element {
    let image : UIImage
    let sound : String
    init (image : UIImage, sound : String) {
            self.image = image
            self.sound = sound
        }
}

let monkey = element(image : UIImage(named: "Baviaan")!, sound : "monkey")
let donkey = element(image : UIImage(named: "Donkey")!, sound : "donkey")
let horse = element(image : UIImage(named: "Horse")!, sound : "horse")
let lion = element(image : UIImage(named: "Leeuw")!, sound : "lion")
let elephant = element(image : UIImage(named: "Olifant")!, sound : "elephant")
let sheep = element(image : UIImage(named: "Sheep")!, sound : "sheep")
let dog = element(image : UIImage(named: "Tekkel")!, sound : "dog")
let chicken = element(image : UIImage(named: "toktok")!, sound : "chicken")
let bird = element(image : UIImage(named: "birds")!, sound : "bird")

var animals = [bird,chicken,dog,donkey,elephant,horse,lion,monkey,sheep]

Pic of error:

screenshot of problem



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source