Xcode 7.3
++ and -- operators
deprecation
programming
software development

The and -- operators have been deprecated Xcode 7.3

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Starting with Xcode 7.3 and Swift 2.2, Apple deprecated the ++ (increment) and -- (decrement) operators. They were removed entirely in Swift 3.0. The change was formalized in Swift Evolution proposal SE-0004, and it reflects a broader philosophy of keeping the language explicit and predictable. This article explains the reasoning, shows the replacement patterns, and gives you practical migration tips.

Why Apple Removed ++ and --

The ++ and -- operators came from C and were familiar to anyone who had written C, C++, Objective-C, or Java. In those languages they have both prefix and postfix forms, each with subtly different behavior:

swift
1// Pre-Swift 3 code (no longer compiles)
2var i = 5
3let a = i++   // a = 5, i = 6  (postfix: return then increment)
4let b = ++i   // b = 7, i = 7  (prefix: increment then return)

The Swift core team identified several problems with these operators:

  1. The prefix/postfix distinction is a source of bugs. Many developers confuse the two or do not realize the return values differ.
  2. They conflate mutation and expression. An operator that both changes a variable and returns a value makes code harder to reason about.
  3. Swift already has a clearer alternative. The += 1 compound assignment operator does the same job without the ambiguity.
  4. They are rarely needed in Swift. Swift's for-in loops and collection methods mean you almost never write C-style for loops where i++ was common.

The += 1 and -= 1 Replacements

The direct replacement for ++ is += 1, and for -- it is -= 1:

swift
1var count = 0
2
3// Incrementing
4count += 1   // count is now 1
5
6// Decrementing
7count -= 1   // count is now 0

These compound assignment operators are explicit about what they do: they add or subtract a value and assign the result back. Unlike ++ and --, they do not have a return value, so there is no prefix-versus-postfix confusion.

Replacing ++ in Loops

In C-style for loops, ++ was used to advance the loop counter. Swift removed C-style for loops in the same release (SE-0007), so the modern approach uses ranges or stride:

swift
1// Old C-style loop (no longer compiles)
2// for var i = 0; i < 10; i++ { ... }
3
4// Modern Swift with a range
5for i in 0..<10 {
6    print(i)
7}
8
9// Counting down
10for i in (0..<10).reversed() {
11    print(i)
12}

Using stride(from:to:by:) for Custom Steps

When you need to increment or decrement by a value other than 1, use stride:

swift
1// Count from 0 to 20 in steps of 5
2for i in stride(from: 0, to: 20, by: 5) {
3    print(i)  // 0, 5, 10, 15
4}
5
6// Count down from 10 to 0 in steps of -2
7for i in stride(from: 10, through: 0, by: -2) {
8    print(i)  // 10, 8, 6, 4, 2, 0
9}

Note the difference between stride(from:to:by:) (excludes the endpoint) and stride(from:through:by:) (includes it). This mirrors the ..< versus ... range operators.

While Loops with Manual Counters

If you genuinely need a mutable counter in a while loop, use += 1 at the appropriate point:

swift
1var index = 0
2let items = ["apple", "banana", "cherry"]
3
4while index < items.count {
5    print(items[index])
6    index += 1
7}

This is clear and leaves no doubt about when the increment happens.

Migration Tips for Existing Codebases

If you are updating legacy Swift 2.x code to Swift 3 or later, the Xcode migrator handles most ++ and -- replacements automatically. For cases it misses or for manual migration:

  1. Search for ++ and -- across your project. In Xcode, use Find and Replace with these patterns.
  2. Replace simple increments/decrements with += 1 or -= 1.
  3. Check for return-value usage. If old code relied on the return value of i++ (postfix), you need to split it into two statements -- capture the current value first, then increment:
swift
1// Old: let old = i++
2// New:
3let old = i
4i += 1
  1. Replace C-style for loops with for-in ranges or stride. The compiler will flag these as errors, so you cannot miss them.

Common Pitfalls

  • Forgetting that += 1 has no return value: Unlike i++, you cannot write let x = (i += 1). If you need the old value, capture it in a separate statement before incrementing.
  • Using stride(from:to:by:) when you mean stride(from:through:by:): The to variant excludes the endpoint. If your loop needs to include the final value, use through instead.
  • Mixing up decrement direction in stride: The by parameter must be negative when counting down. Passing a positive step with a start greater than the end produces an empty sequence with no compiler warning.
  • Not migrating C-style for loops at the same time: The ++ removal and C-style for loop removal happened together. Replace both in one pass to avoid partial migrations that still do not compile.
  • Defining custom ++ operators: You can technically re-add ++ as a custom operator in Swift, but doing so defeats the purpose of the change and confuses other developers who read your code.

Summary

  • Swift removed ++ and -- in Swift 3.0 (deprecated in 2.2/Xcode 7.3) via proposal SE-0004.
  • The replacement is += 1 and -= 1, which are explicit and have no prefix/postfix ambiguity.
  • Use for-in with ranges for simple loops and stride(from:to:by:) or stride(from:through:by:) for custom step sizes.
  • When migrating, watch for code that relied on the return value of postfix ++ -- split it into a capture and an increment.
  • The Xcode migrator handles most conversions automatically, but always review the results manually.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.