1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Using Object-Oriented Programming in Swift

Access Levels

Contents

keyboard_tab
Object-Oriented Programming
2
What is OOP?
PREVIEW6m 49s
3
11
Details
9m 33s

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
1h 22m
Students
15
Description

This course delves into Object-Oriented Programming (OOP), covering its main components, and then putting these into practice by walking you through the creation of an app using OOP.

Learning Objectives

  • Learn about classes and how you can leverage them in your code.
  • Learn concepts such as Enum, Inheritance, and Access Levels
  • Understand how these can be used to build apps

Intended Audience

This course is intended for beginners who want to learn how to build apps using Swift.

Prerequisites

To the most out of this course, you should have some basic understanding of programming and computer science in general.

 

Transcript

Hi, within this lecture, we're going to learn about access levels. So, what are those? They define where we can reach the variable or functions that we have created.

We have five of them. It starts with private, and it goes down to the open. So, let me start with the open and public. We don't generally use them. Why? Because they are not safe to implement. If you make something open, it means that it can be reached from anywhere in the project, and it can be changed from anywhere in the project. Public also can be reached from anywhere, but they cannot be changed. So, why are we not using them? Who uses them anyway? Within the next sections, we're going to work with external libraries. So, we're going to just download some software kit, development kits as the case to our project, for example, in order to work with a cloud server in the case of Firebase. We're going to see how we're going to do that. Don't worry about it. But right now, know that we're going to implement and download some project files into our own project. And this Firebase developers make their files public so that we can reach them and use them in our own project. But other than that, there is no way; there is no logic to implement public and open. Rather, we're going to work private, fileprivate, and internal mostly. So, private means this is actually private. This is special to the class that we have created in.

It cannot be reached from anywhere else. So, if you want to make it private, then you have to be careful because you cannot reach it from somewhere else. And internal is the default one, and fileprivate means you get to reach it from the file. For example, you get to reach it from these Musicians.swift file, not in the class only but from this file. So, let me make an example because you will understand it much better this way. For example, let me make this function sing private. So, if I make this private, it means that I can only reach this sing function within my musicians class. I cannot reach it from anywhere else in my project. So, let's try. I made this private. So, can I call this from main.swift? Or can I call this function from other classes or other service files? I cannot do that. If I do command B or if I try to run this, as you can see, it gives me an error and says that 'sing' is inaccessible due to 'private' protection level. So, James cannot sing right now because I made it private. And in the SuperMusician class also, even though I'm inheriting this, I cannot reach the function sing. So, if I make this private, they cannot call this sing function from anywhere in my project.

So, it doesn't make sense to make this private because I'm inheriting this function; I'm using this function in my objects. So, I believe we should delete this. But there will be some cases that you may want to make things private. For example, if you try to come up with a variable inside of your main.swift or inside of your ViewController, and if you're not going to use that variable inside of this another class, for example, you can make it private. And you can make it for functions as well. For example, in my Musician.swift, let's suppose that I have a test function. I'm just testing something. So, I can print out test, for example. Let's say that we're again working in a big group. So, I'm developing musicians class, and I'm testing something, but I don't want anyone to create a musician object and reach the test function. So, as you can see, they cannot call test from main.swift. So, let me do a command B. As you can see, they can call sing, but they cannot call test. So, this is good. We can make things private if we want to make them really private, if we want to use them only within the current class. And we can make it fileprivate if we want to use it inside of that particular file. So, this is all we want to talk about access levels, and this is all we want to talk about objected oriented programming. So, we're going to stop here. And within the next lecture, we're going to create a model and display all of this information in a real app.

About the Author
Students
921
Courses
55
Learning Paths
3

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics