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 Would you describe yourself as knowledgeable, but struggling when you have to come up with your own code? Fernando Olivares has a new book containing iOS rules you can immediately apply to your coding habits to see dramatic improvements, while also teaching applied programming fundamentals seen in refactored code from published apps.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.