2016-08-23 57 views
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 

} 

bottombottomExtended是相同的长度。我曾尝试使用下面的代码合并,他们成功:

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) 

但它的不合并数组。我检查了数组数。我该如何解决这个问题?

+1

数组是数值类型。你追加不参考底部数组,但它复制内部方向阵列 –

+1

你的代码是否编译?这个'directionArray [enumRawValue] .count'是一个编译错误,因为'Int'没有'count'。 –

+0

@MaxPevsner directionArray有类型[[[CGPoint]]]。 Hilarious404,如果你愿意,用NSArray(引用类型)重写你的代码,但我相信更好的重新思考任务和编写自定义类或结构,取决于预期的行为。 –

回答

-1

我认为你的问题的原因是你正在声明CGPoint数组的数组而不是CGPoint数组。

尝试用:

var bottom = [CGPoint]() 

,并与其他阵列做同样明显

编辑:我不知道为什么这已经downvoted。如果数组的数组意图行为,那么它工作正常。刚做了一个操场来证明它。

enter image description here 很显然,您正在追加元素的数组是一个insideArray(最后一行代码)。原始底部数组未更改,因为directionArray包含底部数组的COPY,而不是原始底部数组,因为此行

directionArray = [right, bottom, left, top]