mardi 5 mai 2015

Swift: obejcts as array

I'm new in Swift and I got problem using an array of objects.

class myClass {
    var test: Int?

    static func testFunc() {
        var array = [myClass] (count: 30, repeatedValue: myClass())
        for i in 0...20 {
            array[i].test = i*2
        }

        for a in 0...20 {
            println(array[a].test)
        }
    }
}

I really have no idea what could be wrong here but my result is always 40 instead of 0 to 40:

Optional(40)
Optional(40)
Optional(40)
ect......

Does anyone know how to solve this problem? Almost seems a bit like a bug.

Regards Nils

Aucun commentaire:

Enregistrer un commentaire