Fill an array with String variable

Hello everyone !

I don't understand, I write this code to fill the array with the string variable value but there are error.. Below my code :

    var Empty_image = ""

    var Abductor_machine_image = "Abductor machine"

    var Abs_crunch_machine_image = "Abs crunch machine"

    var Abs_crunch_oblique_image = "Abs crunch oblique"

    var Abs_floor_oblique_image = "Abs floor oblique"

    var Abs_incline_bench_image = "Abs incline bench"

    var Abs_machine_image = "Abs machine"

    var Abs_roman_chair_oblique_image = "Abs roman chair oblique"

    var Abs_rotation_crunch_image = "Abs rotation crunch"

     let Images3 = [Empty_image, Abductor_machine_image, Abs_crunch_machine_image, Abs_crunch_oblique_image, Abs_floor_oblique_image, Abs_incline_bench_image, Abs_machine_image, Abs_roman_chair_oblique_image, Abs_rotation_crunch_image]

And I have this error for each variable but I don't be able to fix it :

"Cannot use instance member 'Abductor_machine_image' within property initializer; property initializers run before 'self' is available"

Sorry I beginning on swift..

Let's simplify your example:

struct MyWorkout {
    var Abductor_machine_image = "Abductor machine"
    let Images3 = [Abductor_machine_image]
}

This equivalent to the following:

struct MyWorkout {
    var Abductor_machine_image = "Abductor machine"
    let Images3 = [self.Abductor_machine_image]
}

However, there is no self at the time that these properties need to be initialized, so you get that error message. The solution is to defer setting Images3 until self exists:

struct MyWorkout {
    var Abductor_machine_image = "Abductor machine"
    let Images3: [String] // <-- just a declaration, no value provided
    init() { // <-- `self` exists when this initializer is called
        Images3 = [Abductor_machine_image] // <-- the value is provided here instead
    }
}

Polyphonic's answer is correct: Images3 needs to be initialized in init(). Swift will never allow you to set a struct member's value to another struct member's value in the struct declaration itself. This is best illustrated by example:

struct ThisNeverWorks {
    var a = 1
    var b = a    // Error. Cannot do this.
}

struct DoThisInstead {
    var a = 1
    var b: Int

    init() {
        b = a
    }
}

The exception is if right-hand value is a static member:

struct ThisWorks {
    static let a = 1
    var b = a    // This is fine because a is static.
}

Another consideration, however: will the values of Empty_image, Abductor_machine_image, etc. ever change? If not, then consider making them static let constants (or perhaps even an enumeration). Otherwise, this could happen:

struct MyWorkout {
    var Empty_image = ""
    var Abductor_machine_image = "Abductor machine"
    let Images3: [String]

    init() {
        Images3 = [Empty_image, Abductor_machine_image]
    }
}

// ...

var x = MyWorkout()

x.Empty_image = "Something"    // No longer empty.

I would love to help you learn if you're willing to share in more detail what you're trying to do.

Fill an array with String variable
 
 
Q