2
我在我的项目如下排列:追加数组自定义功能不工作
var bottom = [[CGPoint]]() // bottom = [bottom1,...,bottom5]
var left = [[CGPoint]]() // left = [left1,...,left5]
var top = [[CGPoint]]() // top = [top1,..., top5]
var right = [[CGPoint]]() // right = [right1,...,right5]
var bottomExtended = [[CGPoint]]()
directionArray = [right, bottom, left, top]
enum Direction: Int {
case right = 0, bottom, left, top
}
bottom
和bottomExtended
是相同的长度。我曾尝试使用下面的代码合并,他们成功:
for i in 0..<bottom.count {
bottom[i].appendContentsOf(bottomExtended[i])
}
我写了一个通用功能合并数组:
func addToDirectionArray (direction:Direction, addArray:[[CGPoint]]) {
let enumRawValue = direction.rawValue
for i in 0..<directionArray[enumRawValue].count {
directionArray[enumRawValue][i].appendContentsOf(addArray[i])
}
}
我用下面的测试吧:
addToDirectionArray(.bottom, addArray: bottomExtended)
但它的不合并数组。我检查了数组数。我该如何解决这个问题?
数组是数值类型。你追加不参考底部数组,但它复制内部方向阵列 –
你的代码是否编译?这个'directionArray [enumRawValue] .count'是一个编译错误,因为'Int'没有'count'。 –
@MaxPevsner directionArray有类型[[[CGPoint]]]。 Hilarious404,如果你愿意,用NSArray(引用类型)重写你的代码,但我相信更好的重新思考任务和编写自定义类或结构,取决于预期的行为。 –