[英]Multidimensional array of objects bug in swift?
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). 我有一个由项目(对象)组成的多维(2D)数组,我决定使用“自然”方式创建它(因此,我没有使用技巧将1D数组转换为2D,而是直接使用2D)。
An item has a X & Y position on the matrice and the item has also a random type. 一个项目在矩阵上的X和Y位置,并且该项目也具有随机类型。
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. 除了所有项目的Y位置之外,其他所有事情似乎都可以工作...由于Swift处理2D的方式,我需要初始化2D数组,然后正确影响每个项目的值。
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.
然后,当我正确设置项目后验证该项目时,Y位置对于所有项目来说都是唯一的。
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): 和输出(问题以红色显示):
As you can see, during "setting values", X & Y are correct. 如您所见,在“设置值”期间,X和Y是正确的。 But at check, only X is correct and Y is "stuck".
但是在检查时,只有X是正确的,而Y是“卡住”的。 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.
对于所有项目,只有X是正确的,Y和类型是“固定的”。
Your problem is that repeatedValue:Item()
is only evaluated once, it is not evaluated count
times. 您的问题是
repeatedValue:Item()
仅计算一次,而不计算count
次数。 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. 这意味着给定列的每一行中都具有相同的Item,因此在设置值时,您将覆盖前一个值-这就是为什么在打印时获得最后一个值(2)的原因。
You need to use a loop to populate the rows rather than using the count/repeatedValue construct. 您需要使用循环来填充行,而不是使用count / repeatedValue构造。
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()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.