Access Control In Swift
Contents
Introduction
To restrict access code blocks or modules, abstraction is done through access control. Like other programming languages swift also provides option to restrict access to properties, types, functions, and so on. In other programming languages like C# and Java developers are used to working with class member access level modifiers like public
,private
, and protected
. In swift access level modifiers too but they are just a little bit different from other programming languages.
Modules and Source Files
In swift access control is based on the concept of modules and source files.
A module is a single unit of code distribution like a framework or application that is built and delivered as a single unit and that can be imported by another module with swift’s import
keyword.
A source file is a single Swift source code file within a module(a single file within an app or framework). It is common to define individual types in separate source files, a Single source file can contain definitions for multiple types, functions, and so on.
Access Levels
Public
Public access level is the least restriction applied to a member and normally used when writing public interfaces.
Public access level enables entities (properties, types, functions, and so on) to be used within any source file from their defining module, and also in a source file from another module that imports the defining module.
You typically use public access when specifying the public interface to a framework.
Internal
internal access level allows to use any code that in the same module (application or framework) can use the entities.
Internal access enables entities to be used within any source file from their defining module (application or framework), but not in any source file outside of that module (application or framework).
This is a default access level.
Private
Private access restricts the use of an entity to its own defining source file only.
It means that only code in the same source file can use the entity.
Use private access to hide the implementation details of functionality.
Access Control Syntax
public class MyPublicClass { // your code } internal class MyInternalClass { // your code } private class MyPrivateClass { // your code }
public var MyPublicVariable = 0 internal let MyInternalConstant = 0 private func MyPrivateFunction() { // your code }
When use default access level internal, no need to add internal
.
class MyInternalClass { // implicitly internal // your code } let MyInternalConstant = 0 // implicitly internal
Custom Types
public class MyPublicClass { // explicitly public class public var MyPublicProperty = 0 // explicitly public class member var MyInternalProperty = 0 // implicitly internal class member private func MyPrivateMethod() {} // explicitly private class member }
class MyInternalClass { // implicitly public class var MyInternalProperty = 0 // implicitly internal class member private func MyPrivateMethod() {} // explicitly private class member }
private class MyPrivateClass { // explicitly private class var MyPrivateProperty = 0 // implicitly private class member func MyPrivateMethod() {} // implicitly private class member }
public enum CompassPoint { case North case South case East case West }
Subclassing
public class A { private func someMethod() { // super class } } internal class B: A { override internal func someMethod() { // sub class super.someMethod() // call super class method } }
Constants, Variables, Properties, and Subscripts
A constant, variable, or property cannot be more public than its type. It is not valid to write a public property with a private type, for example. Similarly, a subscript cannot be more public than either its index type or return type.
If a constant, variable, property, or subscript makes use of a private type, the constant, variable, property, or subscript must also be marked as private
:
private var MyPrivateInstance = MyPrivateClass()
The example below defines a structure called MyString, which keeps track of the number of times a string property is modified:
struct MyString { private(set) var numberOfEdits = 0 var value: String = "" { didSet { numberOfEdits++ } } } var stringToEdit = TrackedString() stringToEdit.value = "This string will be tracked." stringToEdit.value += " This edit will increment numberOfEdits." stringToEdit.value += " So will this one." print("The number of edits is (stringToEdit.numberOfEdits)") // prints "The number of edits is 3"
If you create a MyString instance and modify its string value a few times, you can see the numberOfEdits property value update to match the number of modifications.
Although you can query the current value of the numberOfEdits property from within another source file, you cannot modify the property from another source file.
This restriction protects the implementation details of the MyString edit tracking functionality, while still providing convenient access to an aspect of that functionality.
I hope you will find this post very useful regarding access control in swift. Let me know if you have any question regarding swift in comment . I will reply to you ASAP.
Have you got cool idea about iPhone App Development? Contact us Now to get free consultation on your idea. Alphanso Tech is rated as one of the best iPhone App Development Company in India.
Thank you for the post!
I’m clearly understand how access control works.
But could you explain, please, WHY we need to use different types of access control?