No '+ =' candidates call the expected type of context result 'Int'

I am updating my Swift code for Swift 3 (really excited) and am still so good. But I really hit one small code that I seem to be unable to update.

I KNOW that I am missing something very simple, but I just don’t see that.

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 } 

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

However, in this context, an error occurs:

No '+ =' candidates call the expected type of context result 'Int'

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

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

But I get the same error.

Can someone point me in the right direction?

+7
ios swift swift3
source share
2 answers

+= does not return a value. You need to overcome this. Fortunately, in your case, this is straightforward and clear than the original:

 column = (column + 1) % numberOfColumns 
+9
source share

Like this:

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

All Articles