Skip to content
← Back to Community
Swift Tutorial
Profile icon
samrcode

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

Printing is a way to put text onto the console.

print("Hello, World!")

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

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:

/* My First Swift App Written by totallygeneric on repl.it */ print("Hello, World!") // prints "Hello, World!"

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:

var myString = "Hello, World!" // makes a string var myInt = 24 // makes an integer var isSwiftFun = true // makes a Boolean value

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

var pieceOfText: String = "This is some text."

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

var myVerySpecialString: String = "Some Text" myVerySpecialString = "Not Some Text" var isSwiftGood = false // very false isSwiftGood = true // that's better

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

let veryImportantString: String = "very important data" veryImportantString = "some other data" // will throw error

Let's talk more about the Dictionary data type.

var myDictionary: Dictionary = [ "vowels": ["a", "e", "i", "o", "u"], "secretCode": "password123", "number": 1, "numbers": [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] ]

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:

// Make dictionary var person: Dictionary = [ "service": "example.com", "name": "Bob", "username": "bob123", "password": "password123", "email": "[email protected]" ] // Print values print(person["service"]) // "example.com" print("Hello, \(person["name"])! Your username is \(person["username"]).") // "Hello, Bob! Your username is bob123." print("Password: \(person["password"]) // "Password: password123" print("E-mail: \(person["email"]) // "E-mail: [email protected]"

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:

// Make dictionary var interestingLetters: Dictionary = [ "vowels": ["a", "e", "i", "o", "u"], "consonants": ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"] ] // Accessing values print("The first vowel: \(interestingLetters["vowels"][0])") print("The second consonant: \(interestingLetters["consonants"][1])")

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.

Concatenation

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:

var myFruit = "banana" var myName = "Bob" var numberOfFruit = 10 print("Hello, \(myName)! You have \(numberOfFruit) \(myFruit)s!") // prints "Hello, Bob! You have 10 bananas!"

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

Voters
Profile icon
Arraf-IntehaInt
Profile icon
DynamicSquid
Profile icon
samrcode