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: Use the func keyword to start the function. Give …

Functions Read More »