Learn to Code via Tutorials on Repl.it!

← Back to all posts
Swift Tutorial

A Swift Tutorial

I am making this tutorial because most of the Swift tutorials here on repl.it don't teach you everything that there is. So, I went out and learned Swift myself, and now I want to teach YOU how to code in Swift!


Printing is a way to put text onto the console.

And it's that simple! All you have to do is use the print() function. A string is a piece of text. To make a string, you use double quotes "". In Swift you can not use single quotes '' to make strings, unlike languages like Python.


Comments are a way to write stuff about your code. You can use them to tell others what your code does, or to remind yourself what a line of code does.

// is a single-line comment. It starts from the // and ends at the end of the line.
/* */ is a multi-line comment. It starts from the /* and ends at the */.

You can choose which one to use. We normally use single=line comments for short comments, and multi-line comments for long comments. Example:

Variables and Data Types

A variable is a container that can hold information. Remember strings, from earlier? Strings are known as a data type. There are different types of data types. Some include:

  • String: Holds text
  • Int: Holds an integer (a number between -2 147 483 648 and 2 147 483 647)
  • Bool: Holds a Boolean value (true or false)
  • Dictionary: A special variable that can basically hold variables inside a variable.

You can make a variable using var. After the var, type the name of the variable, then type the value after an equal sign =. Examples:

You can also type the data type after a colon :. Example:

You can change the value of a variable by redeclaring it without using var. Example:

If you don't want your variable to change, declare the variable using let. let works the same way as var.

Let's talk more about the Dictionary data type.

As we said earlier, dictionaries are basically variables that have variables inside them.

Inside the brackets [], you can make a key.
A key is basically one of the variables inside a dictionary.

First, type the name.
Then, after a colon :, write the data that the key is meant to hold. You can use any type you want.

There is also a type called an array.
An array is a type of dictionary that doesn't use keys, instead uses index numbers, and can only use one data type inside (as far as I know).

To access a dictionary, do the following:

As you can see, you first type the name of the dictionary. In this case, the dictionary is person.
Then, you type inside brackets [] the name of the key you want to access.

If you are working with arrays, do the following:

As you can see, you have to add one more pair of brackets after the ones for the key. Inside the brackets [], type the index number of the value you want to access.

NOTE: Index numbers start at 0.


Concatenating is putting two or more strings or other types of data together. To concatenate, you can use an escape character \ with parentheses (). Inside the parentheses, write the name of the variable. Example:

NOTE: This tutorial isn't finished. I'll probably finish it later. In the meantime, look in the attached repl to find out more.