Canarys | IT Services


Access Control in Swift 3


If you are doing programming for a while, you have often heard of some weird keywords like open,public,internal,file-private and private.

Before we dive into those keywords, we must know one important thing “Module“.
module is just a bundle of code. single Xcode project/framework/bundle is considered as a single module.

Even UIKit is considered as one module. For example, when you try to interact with UIComponents such as UITableView, UIButton, UIViewController, and so on, you have to import the UIKit library/module on the top.


import UIKit

class PrimaryViewController:UIViewController{}

So, we are using code written in UIKit.By default your xcode project doesn’t contain all of those swift/Obj-C files, so you must import.

if you want to import someones code probably you might have used cocoa pods to download the files but you can use those classes within framework by importing module/framework.


import CanarysRecords

Let’s see what apple says about Module:

A module is a single unit of code distribution — a framework or application that is built and shipped as a single unit and that can be imported by another module with Swift’s import keyword.

I think its enough of introduction about Module.

Now its time to get into some real world scenario “Access Control“.

Why Access Control?
well, main idea behind access control is to stop mess arround with what we have just imported.
For Example, UIKit Engineers at Apple don’t want ios developers to modify some of theirs classes,methods,variables and this restrictions we can impose with the help of Access Control.

Stil in confusion?
lets see this way, Imagine you gave your Mac to your 7 year old son to use.But your kid begins to open up the parts of entire Mac and try to “Customize” things.Same way, Apple engineers don’t want developers to mess arround with their code.So they have created some sort of restrictions/barrier to block us from having to their source code.That’s the reason it’s called, “Access Control”.

Like i mentoned above we have 5 types of Access Control in swift. Let’s begin with one by one,

Open is a Least Control.
An open class is accessible and subclassable outside of the defining module. An open class member is accessible and overridable outside of the defining module.

For Example:


Great, but how is this possible that we can subclass?To get the solution, You can option+Click on UITableview class
here what you can see

open class UITableView : UIScrollView, NSCoding, UIDataSourceTranslating {} 

so,Open is to provide you access from defining module UIKit and even you can subclass as shown above helloTableView.

Public is almost similar to Open but not subclassable outside of the defining module.A public class member is accessible but not overridable outside of the defining module.

For Example:

public protocol UITableViewDelegate : NSObjectProtocol, UIScrollViewDelegate {}

From your app bundle/Xcode project you have access to UITableView. But we can’t subclass UITableViewDelegate in our own.
So it can only be overridden by subclasses within the module where they are defined.

Well all veriables,class and functions are internal by default.Those that are marked with internal can be used only within any source files from their defining module but not in any source file outside of that module.

whatever the internal functions that are created by apple developers can not be accessed by other ios developers.

For Example: 

internal func demofunction(){

demofunction can’t accessed outside the defining module even we import that module.

This access specifier can be used only if you want your function/class/variables to be used within one single swift file.

Below code will give you a clear picture of difference between File-Private & Private (Private access specifier we are going to look in a short time).

For Example:

In demo.swift file

class A {
    private func foo() {}
    fileprivate func bar() {}

    func baz() {

extension A {
    func test() {
        foo() // error: use of unresolved identifier 'foo'

let a = A() // error: 'foo' is inaccessible due to 'private' protection level

The private foo method is accessible only within the scope of the class A { … } definition. It is not even accessible from an extension to the type 

The file-private bar method is accessible from the same source file.

Finaly, There is no way for you to use/access when you “escape” out of classes, functions, or methods achived by Private.



class Demo{

    private var checkME = "Got You"

Demo() //No error
Demo().checkME ///Error

That’s it. I hope You’ve fully understood the concepts of access specifiers. so no more wories again if you see open,public,internal,file-private and private keywords.

Leave a Reply

Your email address will not be published. Required fields are marked *

Reach Us

With Canarys,
Let’s Plan. Grow. Strive. Succeed.