0

I have a multidimensional (2D) array of items (object) and I decided to use the "natural" way to create it (so instead of using the trick to transform a 1D array into 2D, I go 2D directly).

An item has a X & Y position on the matrice and the item has also a random type.

Everything seems to work except for the Y position of all the items... Because of how Swift handles 2D, I needed to initialize the 2D array then affect the values correctly to each item.

I do verify the value that I affect to the items, they work. Then when I verify the item after it has been correctly set, the Y position is like unique to all the items.

class Matrix {
    var nbCol: Int
    var nbRow: Int
    var items: [[Item]]

    init(nbCol: Int, nbRow: Int) {
        self.nbCol = nbCol
        self.nbRow = nbRow
        items = Array<Array<Item>>()
        //Initialize
        for col in 0..<Int(nbCol) {
            items.append(Array(count: nbRow, repeatedValue: Item()))
        }
        //Affect the correct values to each item
        createItems()
    }

    func createItems() {
        //Assign X, Y & type values to the item
        for x in 0..<Int(nbCol) {
            for y in 0..<Int(nbRow) {
                items[x][y].setItem(x, y: y, type: ItemType.random())
                println("Create for (\(x), \(y)): (\(items[x][y].x), \(items[x][y].y))")
            }
        }
        //Verify the values
        for x in 0..<Int(nbCol) {
            for y in 0..<Int(nbRow) {
                println("Check for (\(x), \(y)): (\(items[x][y].x), \(items[x][y].y))")
            }
        }
    }
}

And Item (part of it) is :

class Item: Printable {
    var x: Int
    var y: Int
    var type: ItemType  //Enum

    func setItem(x: Int, y: Int, type: ItemType) {
        self.x = x
        self.y = y
        self.type = type
    }

}

And the output (with the problem in red):

Console output

As you can see, during "setting values", X & Y are correct. But at check, only X is correct and Y is "stuck". Am I doing something wrong ?

EDIT : By the way, all items has also the same type. Only X is correct, Y & type are "fixed" for all items.

1
  • [In reply to "Can we have the full code"] Sure thing : gist.github.com/BabyAzerty/4e4aaa3cae6838f4b050 But I believe @Paulw11 pointed out the problem, yet I don't know how to code a solution for it. Commented Dec 1, 2014 at 23:54

1 Answer 1

1

Your problem is that repeatedValue:Item() is only evaluated once, it is not evaluated count times. This means that you have the same Item in each row of a given column, so when you set the values you are overwriting the previous one - which is why you get the last value (2) when you print it.

You need to use a loop to populate the rows rather than using the count/repeatedValue construct.

init(nbCol: Int, nbRow: Int) {
    self.nbCol = nbCol
    self.nbRow = nbRow
    items = Array<Array>()
    //Initialize
    for col in 0..<Int(nbCol) {

         var colArray=Array<Item>()

          for row in 0..<Int(nbRow) {
             colArray.append(Item())
          }

        items.append(colArray)
    }
    //Affect the correct values to each item
    createItems()
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh I understand the problem ! But how should I proceed a loop for the rows in swift ? I tried to do it as anyone would do it in C/C++, but in Swift, you can't create an array with, let's say, 12 fixed rows. And I can't put nil instead of Item() in the repeatedValue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.