Learn

In this lesson, we will explore some special types of properties we can use in structures as well as how access control works in Swift, and how to apply it in the context of structures.

Let’s begin by diving into access control! As the name implies, it allows us to control access to properties and methods within our code. By implementing different levels of access control on properties and methods, we determine which other parts of the code can read their values, write to them, and execute methods. Access control helps us protect sensitive information from being read or modified, making our code safer and more robust.

It’s important to understand the boundaries that define the scope of access control. These boundaries are delineated by modules, source files, and finally struct, class, or enum definitions within source files. We will cover classes in a later lesson; for now, we will focus on structures.

A module is an entire app or framework. Technically, it is defined as a build target for the Swift compiler. Your app may consist of one or more modules; they are like building blocks and can be integrated using the import keyword at the top of a source file. As previously we saw in the Libraries article, Foundation is an example of a module that is very commonly imported to Swift applications.

Source files are exactly what they sound like. They are the files that your source code is contained in. A module typically consists of multiple source files.

To help us understand the purpose and mechanics of access control, let’s think about how information is managed in a company. In a typical large company, we might have some information that is shared with the public, some information that is for anyone who works for the company, some information that can only be shared within a division of the company, and some information that can only be shared within an office of a division. In this analogy, modules are companies, source files are divisions, and structures are offices. Take a look at the image on the right to get a visual of this analogy. Let’s use this analogy to drill down into each level of access control.

Instructions

Continue

Take this course for free

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?