No '+=' candidates produce the expected co

2019-04-25 07:10发布

This question already has an answer here:

I've been updating my Swift code for Swift 3 (really excited) and so far so good. But I did tumble accros one little bit of code I can't seem to update.

I KNOW I am missing something very simple, but I just can't see what.

Here is what I have in Swift 2.2:

var column = 0

[...]

for item in 0 ..< collectionView!.numberOfItemsInSection(0) {
    [...]

    column = column >= (numberOfColumns - 1) ? 0 : ++column
}

The ++column is of course being deprecated in Swift 3 in favor of column += 1

However, in THIS context, it produces an error:

No '+=' candidates produce the expected contextual result type 'Int'

Since this line of code (column = column >= (numberOfColumns - 1) ? 0 : column += 1) produces an error, I tried the following:

var newCol = column
column = column >= (numberOfColumns - 1) ? 0 : newCol += 1

But I get the same error.

Could someone point me in the correct direction?

标签: ios swift swift3
2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-04-25 07:51

+= does not return a value. You need to break this out. Luckily in your case that's straightforward and clearer than the original:

column = (column + 1) % numberOfColumns
查看更多
够拽才男人
3楼-- · 2019-04-25 07:58

Like this:

column = column >= (numberOfColumns - 1) ? 0 : column + 1
查看更多
登录 后发表回答