以前在Swift Beta 2中运行良好的代码不再在Beta 4中编译。有人可以解释原因吗?
for dx in [-1, 0, 1] { // Fine
for dy in (dx == 0) ? [-1, 1] : [0, (xIndex % 2) == 0 ? -1 : 1] { // Now fails
// ...
}
}
给出的错误是"类型'序列'不符合协议' _Sequence'"。我知道如何修复它(只是显式设置内部范围而不是使用?运算符),但为什么现在这是非法表达式呢?
完整的功能是:
func tilesAdjacentTo(#xIndex: Int, yIndex: Int) -> [HexTile] {
var adjacent = [HexTile]()
// We want to do this, where dy is -1 for even columns and +1 for odd
// adjacent += self.pieces[xIndex + 0][yIndex + 1]
// adjacent += self.pieces[xIndex + 0][yIndex - 1]
// adjacent += self.pieces[xIndex + 1][yIndex + 0]
// adjacent += self.pieces[xIndex + 1][yIndex + dy]
// adjacent += self.pieces[xIndex - 1][yIndex + 0]
// adjacent += self.pieces[xIndex - 1][yIndex + dy]
for dx in [-1, 0, 1] {
for dy in (dx == 0) ? [-1, 1] : [0, (xIndex % 2) == 0 ? -1 : 1] {
let x = xIndex + dx
let y = yIndex + dy
if (x > 0) &&
(x <= self.maxX) &&
(y > 0) &&
(y <= self.maxY)
{
adjacent += self.pieces[x][y]
}
}
}
//println("Adjacent \(adjacent.count)")
return adjacent
}