Swift 1.2"无法表达元组转换"错误

时间:2015-03-04 22:33:02

标签: ios swift

这个方法在Swift的最后一个稳定版本中运行良好,但它不会在Swift 1.2中编译:

  final func rotateBlocks(orientation: Orientation) {
    if let blockRowColumnTranslation:Array<(columnDiff: Int, rowDiff: Int)> = blockRowColumnPositions[orientation] {
        for (idx, (columnDiff:Int, rowDiff:Int)) in enumerate(blockRowColumnTranslation) {
            blocks[idx].column = column + columnDiff
            blocks[idx].row = row + rowDiff
        }
    }
}

这一行:

for (idx, (columnDiff:Int, rowDiff:Int)) in enumerate(blockRowColumnTranslation) {

引发以下错误:

  

&#34;无法表达元组转换&#34;(index:Int,element:(columnDiff:Int,rowDiff:Int))to&#34;(Int,(Int,Int))&#34;

关于这里发生了什么以及如何解决的任何想法?

5 个答案:

答案 0 :(得分:3)

我会使用typealias来简化,但以下编译没有错误。

var row: Int = 0
var column: Int = 1
struct block {
    var column: Int
    var row: Int
}
var blocks = [block]()

enum Orientation { case Up; case Down; }
typealias Diff = (columnDiff: Int, rowDiff: Int)
typealias DiffArray = Array<Diff>
typealias DiffArrayDict = [Orientation: DiffArray]

var blockRowColumnPositions = DiffArrayDict();

func rotateBlocks(orientation: Orientation) {
    if let blockRowColumnTranslation: DiffArray = blockRowColumnPositions[orientation] {
        for (idx, diff) in enumerate(blockRowColumnTranslation) {
            blocks[idx].column = column + diff.columnDiff
            blocks[idx].row = row + diff.rowDiff
        }
    }
}

答案 1 :(得分:3)

我遇到了同样的事情,并且能够通过为元组添加element:标签来实现这一点:

for (idx, element: (columnDiff: Int, rowDiff: Int)) in enumerate(blockRowColumnTranslation) {
    blocks[idx].column = column + element.columnDiff
    blocks[idx].row = row + element.rowDiff
}

答案 2 :(得分:1)

对我来说看起来像一个Swift错误。更一般地说,这是被破坏的:

let pair = (a: 1, b: 2)
// normally those named elements don't matter, this is fine:
let (x,y) = pair
// but add a bit of nesting:
let indexed = (index: 1, pair)
// and, error: 
let (i, (x,y)) = indexed
// cannot express tuple conversion '(index: Int, (a: Int, b: Int))' to '(Int, (Int, Int))'

我尝试从数组的元组声明中删除类型名称(即Array<(Int,Int)>而不是Array<(columnDiff: Int, rowDiff: Int)>),看看是否有帮助。

在其他可能相关的新闻中,这似乎会破坏1.2编译器:

let a: Array<(Int,Int)> = [(x: 1,y: 2)]

答案 3 :(得分:1)

谢谢你们!我结束了只是将其重写为for-loop ..这并不令人兴奋,但似乎工作正常:

  final func rotateBlocks(orientation: Orientation) {

    if let blockRowColumnTranslation:Array<(columnDiff: Int, rowDiff: Int)> = blockRowColumnPositions[orientation] {

        for var idx = 0; idx < blockRowColumnTranslation.count; idx++
            {
            let tuple = blockRowColumnTranslation[idx]
            blocks[idx].column = column + tuple.columnDiff
            blocks[idx].row = row + tuple.rowDiff
        }
    }
}

答案 4 :(得分:-1)

final func rotateBlocks(orientation: Orientation) {
    if let blockRowColumnTranslation = blockRowColumnPositions[orientation] {
        for (idx, diff) in enumerate(blockRowColumnTranslation) {
            blocks[idx].column = column + diff.colunmDiff
            blocks[idx].row = row + diff.rowDiff
        }
    }
}