Swift for Beginners

Dictionaries

In Swift, a dictionary is a container for storing key-value pairs. In layman’s terms, you can use a dictionary to store unique items that are related to some values. For instance, let’s create a dictionary that maps a student ID to the name of the student: In this chapter, you learn how to use dictionaries …

Dictionaries Read More »

Arrays

Swift arrays are a very common data type used to organize your app’s data. You can store multiple elements into an array, such as integers or strings. For example, here is an array of numbers: With arrays, you can do a lot of useful tasks, such as add, remove, or update the elements. You can …

Arrays Read More »

Optionals

In Swift, you can use optionals to represent variables and constants that could be nil or a value. Any data type in Swift can be made optional by adding a question mark after the data type name. For instance, let’s create an optional String: The value of this string is now nil. To use an …

Optionals Read More »

Class Inheritance

In Swift, you can use class inheritance to introduce a parent-child hierarchy to your program. The idea is that the inherited child class has all the properties and attributes of the parent class. As an example: Output: Inheritance is a feature that only classes have in Swift. For example, you cannot inherit a struct. In …

Class Inheritance Read More »

Classes

In Swift, a class acts as a blueprint for creating objects. For example, here is a class that represents a person: And here you create a Person object: In this guide, you learn how to create objects using classes in Swift. Also, you are going to learn what is the distinction between a struct and …

Classes Read More »

Structs

In the previous chapters, you have worked with data types like Int, String, Double. But did you know you can also implement your own custom data type? In Swift, one way to do this is by using a struct. For example: Output: In this chapter, you are going to learn how to create your own …

Structs Read More »

Functions

In Swift, a function is a reusable block of code with a name. This function can then be called to perform a specific task with different inputs. When you call a function, you execute the code inside that function. To create a function, you need to: For example, let’s create a function that greets a …

Functions Read More »

For Loops

In Swift, you can use a for loop to repeat code. For example, let’s use a for loop to calculate the sum from 0 to 10: Output: Loops are extremely common in Swift and other programming languages. As you can imagine, when working with data, you need to be able to repeatedly perform the same …

For Loops Read More »

Switch Statement

In Swift, you can use the switch statement to turn lengthy if-else statements into more concise and structured expressions. For example, take a look at this if-else statement. There is a lot of repetition and the code is quite long and somewhat verbose. In this case, you can tidy up the code by replacing the …

Switch Statement Read More »