22

After update to Xcode 7.3, there are bunch of warnings showing in my project.

'++' is deprecated: it will be removed in Swift 3

Any idea to fix this warning ? Any reasons why the ++ and -- will be deprecated in the future ?

Ketan P
  • 4,259
  • 3
  • 30
  • 36
Zigii Wong
  • 7,766
  • 8
  • 51
  • 79
  • Definitely a duplicate... although that question is about the _beta_ (and is dated Feb 3), while this one is about the _official release_ (dated Mar 23, days after Xcode 7.3 hit the Store)... What does one do in such a case? – Nicolas Miari Mar 23 '16 at 09:59
  • 1
    @NicolasMiari Just vote to close it. It's still Xcode 7.3, we can only edit the question to remove "beta". – Sulthan Mar 23 '16 at 10:02

1 Answers1

46

Since Swift 2.2, you should use += 1 or -= 1 instead.

And after looking up Swift's evolution, there are some reasons for removing these operators:

  1. These operators increase the burden to learn Swift as a first programming language - or any other case where you don't already know these operators from a different language.

  2. Their expressive advantage is minimal - x++ is not much shorter than x += 1.

  3. Swift already deviates from C in that the =, += and other assignment-like operations returns Void (for a number of reasons). These operators are inconsistent with that model.

  4. Swift has powerful features that eliminate many of the common reasons you'd use ++i in a C-style for loop in other languages, so these are relatively infrequently used in well-written Swift code. These features include the for-in loop, ranges, enumerate, map, etc.

  5. Code that actually uses the result value of these operators is often confusing and subtle to a reader/maintainer of code. They encourage "overly tricky" code which may be cute, but difficult to understand.

  6. While Swift has well defined order of evaluation, any code that depended on it (like foo(++a, a++)) would be undesirable even if it was well-defined.

  7. These operators are applicable to relatively few types: integer and floating point scalars, and iterator-like concepts. They do not apply to complex numbers, matrices, etc.

Finally, these fail the metric of "if we didn't already have these, would we add them to Swift 3?"

Please check out Swift evolution for more info.

Community
  • 1
  • 1
Zigii Wong
  • 7,766
  • 8
  • 51
  • 79