If you want to add 5 to a
score variable, you’re perfectly able to write
score = score + 5 if you want. Swift also allows us to write
score += 5 to get the same result, but why?
Well, it isn’t for performance reasons – under the hood, Swift turns
score += 5 into
score = score + 5 just as if we had written it.
Instead, they are just available as a shorthand: a simpler way to write the same thing. This means less typing for us, less code to read, and also less chance of making mistakes – a win all around!
SPONSORED Are you tired of wasting time debugging your Swift app? Instabug’s SDK is here to help you minimize debugging time by providing you with complete device details, network logs, and reproduction steps with every bug report. All data is attached automatically, and it only takes a line of code to setup. Start your free trial now and get 3 months off exclusively for the Hacking with Swift Community.
Link copied to your pasteboard.