Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift

swift3

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?

like image 441
Benjamin Avatar asked Mar 31 '16 16:03

Benjamin


2 Answers

+= 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
like image 198
Rob Napier Avatar answered Oct 10 '22 18:10

Rob Napier


Like this:

column = column >= (numberOfColumns - 1) ? 0 : column + 1
like image 38
matt Avatar answered Oct 10 '22 19:10

matt