Swift Tutorial Part 1: Expressions, Variables and Constants

Welcome to our mini-series on getting started with programming in Swift! In this series, you’ll learn some Swift programming basics using playgrounds. By Lorenzo Boaro.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Using Meaningful Names

Always try to choose meaningful names for your variables and constants. Good names, like good comments, can make your code easier to read.

A good name specifically describes what the variable or constant represents. Here are some examples of good names:

  • personAge
  • numberOfPeople
  • gradePointAverage

Often, a bad name is simply not descriptive enough. Here are some examples of bad names:

  • a
  • temp
  • average

The key is to ensure that you’ll understand what the variable or constant refers to when you read it again later. Don’t make the mistake of thinking you have an infallible memory! It’s common in computer programming to look back at your own code as early as a day or two later and have forgotten what it does. Make it easier for yourself by giving your variables and constants intuitive, precise names.

Also, note how the names above are written. In Swift, it is common to camel case names, explained below. For variables and constants, follow these rules to properly case your names:

  • Start with a lowercase letter.
  • If the name is made up of multiple words, join them together and start each subsequent word with an uppercase letter.
  • If one of these words is an abbreviation, write the entire abbreviation in the same case (e.g., sourceURL and urlDescription).

In Swift, you can even use the full range of Unicode characters. For example, you could declare a variable like so:

var 🐶💩: Int = -1

That might make you laugh, but use caution with special characters like these. They are harder to type and, therefore, may end up causing you more pain than amusement.

Bad choice

Special characters like these probably make more sense in data that you store rather than in Swift code.

Increment and Decrement

A common operation that you will need is to be able to increment or decrement a variable. In Swift, this is achieved like so:

var counter: Int = 0

counter += 1
// counter = 1

counter -= 1
// counter = 0

The counter variable begins as 0. The increment sets its value to 1, and then the decrement sets its value back to 0.

These operators are similar to the assignment operator (=), except that they also perform an addition or subtraction. They take the current value of the variable, add or subtract the given value, and assign the result to the variable.

In other words, the code above is shorthand for the following:

var counter: Int = 0

counter = counter + 1
// counter = 1

counter = counter - 1
// counter = 0

Similarly, the *= and /= operators do the equivalent for multiplication and division, respectively:

var counter: Int = 10

counter *= 3 // Same as counter = counter * 3
// counter = 30

counter /= 2 // Same as counter = counter / 2
// counter = 15

Where to Go From Here?

You can download the final playground using the Download Materials button at the top or bottom of this tutorial. To improve your Swift skills, you will find some mini-exercises to complete. If you are stuck or you need some help, feel free to take advantage of companion solutions.

In this tutorial, you’ve only dealt with numbers — both integers and decimals. Of course, there’s more to the world of code than that! In the next part of the series, you’re going to learn about more types such as strings, which allow you to store text.

The next part in this tutorial series deals with types and operations; continue on to Part 2: Types and Operations to carry on with your Swift adventures!

If have any questions or comments, please tell us in the discussion below!

swift-apprentice

This tutorial was taken from Chapters 1 and 2 of Swift Apprentice, Fourth Edition, available from the raywenderlich.com store.

Check it out and let us know what you think!