Properties and Access Control
Learn how to use properties and access control to protect information and respond to changes.
StartKey Concepts
Review core concepts you need to learn to master this subject
Access Control
Access Levels
Private Properties and Methods
Read-only Computed Properties
Property Observers
Private Setters
Static Properties and Methods
Extensions
Access Control
Access Control
Access control specifies which methods and properties can be accessed from outside that scope of a structure, file, or module. Access control makes it possible to hide implementation details and protect properties from being changed at unexpected times.
- 1In 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 beg…
- 2Public We’ll start with public, the most open level, which grants access to a property or method from inside or outside the module. Relating it to the company analogy, since modules are compani…
- 3As we learned in the previous exercise, private is the most restrictive level of access control and can be applied to properties or methods. When we make a property private, we make it so that its …
- 4There are some cases in which we may not want to expose methods outside of the scope of the structure such as when the method might access or modify some sensitive data. For those cases, we can use…
- 5There is another way we can access private properties inside of a structure: computed properties. Computed properties are a special type of property that don’t actually store a value directly, rath…
- 6In addition to getting computed properties, there may be cases where it would be convenient to set the computed property as well. Recall that computed properties don’t store values directly, so we’…
- 7Another built-in feature of properties we might find useful is property observers. If access control is a lockset for our properties, property observers are the security cameras. Specifically, prop…
- 8Oftentimes, we find ourselves being more concerned with data being changed and not so concerned about who can read it. When this is the case, it is possible to implement a private setter. A private…
- 9Up until this point, we have always associated properties with an instance of a struct. However, it is also possible to define a property that is associated directly with the struct itself. Such …
- 10When writing a struct, class, or enum it may be helpful to divide the code into several different sections. By using an extension, you can continue writing the definition of a struct, class, o…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory