Access Control in Swift
What is : private |file-private |internal |public|open
Introduction :
In this Article we will discuss and implement t Access control in swift. Access control is so important to understand for our code safety .
Before we dive in to access control we need to know some basic
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.
Source File :
A source file is a single Swift source code file within a module
What is Access level:
Access control is restriction of access code from another scope or module.This feature hide the implementation of code from another scope or module.We can assign single access level with in any individual type like class ,structure ,enum and as well as to properties, methods, initialisers, and subscripts belonging to those types.
Why Need Access Control:
- Use code with restriction for the less indexation time and completion time.
- Unnecessary access from another module and scope
- For Code Safety
Type of Access Control:
Swift provide Five type of access Control we will define and implement those after sometime and they are :
- Private
- fileprivate
- internal
- public
- open
This access level are from highest restriction to lowest restriction .Lets Talk about this :
private:
This access level is only for with in its scope .Like if we make a class and with in this class we make a private variable than we can access this variable online with in this scope on other word only with in enclosing area.In swift 4 we can access in extension of same file .Use private access to hide the implementation when those details are used only within a single declaration
Design a Class with the name VoterId and make a private variable with in this class called idName with access level of private .
class VoterId{
private var idName:Stringinit(idName:String){
self.idName = idName
}
}
lets create a instance with this class
let voterId = VoterId(idName: “ekramul hoque”)let name = voterId.idName // error
lets see XCode giving us error because of voter it is only accessible from the its scope .
We can Give an good example with singleton and private initialiser.
DESIGN A CLASS
class Network {
static let sharedInstance = Network()var url = "google.com/api"private init(){}}
here we create a Network class and initialise it as a private so we cant create new instance of its .
File-private:
This access restriction is only own source file.With in entire file we can acccess only .Like if we make a file with file-private access level than we can only access this from its own file .Use file-private access to hide the implementation details of a specific piece of functionality when those details are used within an entire file.lets create an example and make it clear .
create an single view App project in Xcode and create an swift file with the name myViewController :
Here is myViewContoller which subclass of UIViewController.Now If we try to make its instance to our another viewController than it will not work compiler will give error like this ..
Compiler didn’t even detect we have an file with this name because or its File-private.But We can create its instance in its own source file like
internal
internal is default access level . If we don’t define access level than its internal access level .So Every where is internal where we don’t assign any access level.
public:
open access enable us to used code with in any source file from their defining module and we can access source file from another source file .Usually use open access when build a framework which can be imported.If we See The UIKit Framework functionality such as
public init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?)
We see it have public access level and we can use this method outside the file and where we import the module.We can use every where but we can Not override.
we can not override any type which is public
Open:
Open is most less restricted access level.Open access enable to use one source file to another source file .We can access from another module where its imported . UIKit most classes are Open we can subclass and override it .
Difference Between Open and public :
Subs-class public:
class with public access or any more restricted access level can be subclassed only within the module where there are defined .
overridden public:
Class members with public access, or any more restrictive access level, can be overridden by subclasses only within the module where they’re defined.
Subs-class open:
Open classes can be subclassed within the module where they’re defined, and within any module that imports the module where they’re defined.
overridden public:
Open class members can be overridden by subclasses within the module where they’re defined, and within any module that imports the module where they’re defined.
Simply:
public access level or more restricted access level like internal , file private etc can be subclass or override only with in module where thy are defined.But Open access level can be subclassed and override with in the module where thy designed and also outside the module where thy are imported .
Conclusion :
Thank you for staying with me .Feel free to share and clap on me .Thank you 😍 😍
About Me : I am swift lover and iOS Developer From Bangladesh.