没有'+ ='候选者产生预期的上下文结果类型'Int' [英] No '+=' candidates produce the expected contextual result type 'Int'
问题描述
我一直在为Swift 3更新我的Swift代码(非常兴奋),到目前为止一直很好。
但我做了一些代码,我似乎无法更新。
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.
以下是我在Swift 2.2中的内容:
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
}
++列
当然在Swift 3中被弃用,而不是列+ = 1
The ++column
is of course being deprecated in Swift 3 in favor of column += 1
但是,在这种情况下,它产生一个错误:
However, in THIS context, it produces an error:
没有'+ ='候选产生预期的上下文结果类型'Int'
No '+=' candidates produce the expected contextual result type 'Int'
由于这行代码( column = column> =(numberOfColumns - 1)?0:column + = 1
)产生错误,我尝试了以下内容:
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
但我得到同样的错误。
有人能指出我正确的方向吗?
Could someone point me in the correct direction?
推荐答案
+ =
不返回值。你需要打破这个。幸运的是,你的情况比原来简单明了:
+=
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
这篇关于没有'+ ='候选者产生预期的上下文结果类型'Int'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!