Swift Basics

Swift Basics

Why Swift?

Since its release in 2014, Swift has become a popular choice for developing iOS applications and is gaining ground in other areas such as machine learning and server-side programming. Swift is safe, fast, and easy to read, making it a great choice for new developers and experienced devs alike.

In this post, I want to help you get started writing your first lines of Swift code. We'll discuss how to get started with Xcode, how to create constants and variables, and how to use some basic data types to store information. Let's get started!

Getting Started

All you need to get started is a Mac and the application, Xcode. You can download Xcode from the App Store. If you'd like to try out any of the code snippets for yourself, simply open up a playground file by opening Xcode and following the menu path File -> New... -> Playground... Select a Blank playground, give it a name, and save it wherever is convenient for you. You should now see a playground file with just two lines of code, import Cocoa and var str = "Hello, playground". This is the file that we will use to add all of our code, so let's start making some changes to our playground.

Constants and Variables

If this is all completely new to you, let's look at some basic Swift syntax to start our discussion on constants and variables. Add this to your playground:

let myName = "John"
let myBirthdayMonth = "October"
var myAge = 27

What have I just done? I have declared three pieces of information that describe me. In Swift, we use the let keyword to create what is known as a Constant. As the word suggests, Constants are things that aren't going to change, my name is John and my birthday is in October. Now, if I want to have something that can change, I will use what is known as a Variable. To create variables in Swift, like I did with the myAge variable, we use the Swift keyword var.

To show you that I'm not lying about constants being constant and variables being variable, try changing your myName constant to a different value like so:

myName = "Bob"

So what happened? You should see an error like the one pictured below. image.png

This error is letting you know that you cannot change the value of a constant, you need to change it to a variable if you want to make a change.

With this in mind, do you know what will happen if you try changing your variable myAge? Give it a try. Change the myAge variable to any other number you want. What happens? No errors. You are free to change variables whenever you want and Xcode won't complain.

String and Integer Types

Swift has a number of different types that you can use to store different pieces of data. If we look back at our previous variable and constants, we have two constants of type String and a variable of type Integer. So what are these types? Strings are a series of characters and are represented in Swift by the String type. Integers are whole numbers with no fractional components, like 5, 10, 27, 130, etc. Integers are represent in Swift by the Int type. Looking back at our examples from above, we could rewrite each of these lines explicitly declaring the type of the variable or constant, like so:

let myName: String = "John"
let myBirthMonth: String = "October"
var myAge: Int = 27

To explicitly declare a type, you simply add a colon and specify the type before the equals sign. Now you might be wondering, do I have to declare the type of the constants and variables I use? The simple answer, no. The Swift compiler is smart enough to infer the data type you intend to use based on what you store in your variable or constant. However, there will be instances in your Swift development journey where explicit declarations will be necessary, but those cases will have to wait for a future post. For now, it is safe to ignore explicit declarations.

Other Types

Swift has a number of other data types that are made available to developers. Some additional types include Double, Float, Boolean, and Character. If we look at each of these:

  • Double can store numbers with a decimal or fractional component, like 5.25, and can be declared using the Swift keyword Double
  • Float is similar to a Double in that it can store numbers with a fractional component and can be declared using the Swift keyword Float. The (simple) difference between a Float and a Double is a Double can store larger numbers than a Float. It is recommended to use Doubles.
  • Boolean is a binary choice, it can either be True or False and is declared using the Swift keyword Bool. Booleans become especially useful once you start working with if-else code blocks.
  • Character can store a single-character string literal, like a or J, and is declared using the Swift keyword Character.

I encourage you to check out the official Swift language documentation here to get an even more in-depth look at the various data types available to you in Swift.

Conclusion

I hope this gives you a basic understanding of the different data types and how to create constants and variables in Swift. Play around with creating new variables and constants, making changes, and figuring out what will cause errors and what won't. The best way to get better at writing Swift is to write Swift.

Be on the lookout for future articles where I will discuss more Swift basics as well as some more advanced topics, such as making network requests using Swift. Happy coding!