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

南笙酒味 提交于 2019-12-22 04:45:14

问题


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?


回答1:


+= 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



回答2:


Like this:

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


来源:https://stackoverflow.com/questions/36338866/no-candidates-produce-the-expected-contextual-result-type-int

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!