Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKatie Blount
3,043 Pointscaution sign
I'm getting a caution sign that says ++ is deprecated and being removed in swift 3. Is it best practice to not use it?
2 Answers
Steven Deutsch
21,046 PointsHey Katie Blount,
Yes the ++ and -- operators will no longer be available in Swift 3.0. It would be best to stop using these operators and learn how to adapt ahead of time. You can use the += and -= operators as a sort of replacement. It's not a perfect replacement because these operators return void and they do not have prefix and postfix forms. Therefore, you will most often have to write code that is 2-3 lines using the += operator where you would normally use the ++ or -- operator on a single line.
// Swift 2 version
var initialValue = 10
let newValue = ++initalValue
print(newValue) // will print 11
// Swift 3 version
var initialValue = 10
initialValue += 1
let newValue = initalValue
print(newValue) // will print 11
As I mentioned earlier, the += does not return a value. Therefore, we can't assign the result of the operation to a variable or constant on the same line. You would have to assign the modified value to a variable or constant on the next line.
Hope this helps. Good Luck!
Rodrigo Chousal
16,009 PointsHey Katie,
It's not that it's best practice not to use it (although it is because of the fact it's being deprecated), but it's more about keeping Swift modern and concise. The ++ and -- operators are being replaced with += 1 and -= 1. This is mainly because Swift doesn't have much use for these operators since they were used in C and were carried over "without much thought". Read more on why this change was made here.