Access Control in Swift

Swift like other programming languages provides option to restrict access to classes, functions, variables, structs, enums etc applying the required Access Control. These restrictions are based on each module, as per Apple documentation a module is defined as

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.

Access Levels

There are three types of Access Control restriction that can be applied to individual types inside a module

public – The least restriction applied to a member and normally used when writing public interfaces

internal – Default access level and a member with this restriction can be accessed only within the module.

private – Most restricted access level and member with this restriction can be accessed only within the source file.

Check out more on Access Controls in Apple documentation Guiding Principles of Access Levels

Now let us see a demo on how these access levels can be used with in Swift projects or frameworks.

Access Control Demo

Create a project using Single View Application template (though this is going to be non-UI demo). Add a new swift file with name as Greetings.swift and following implementation.

[code language=”swift”]class Greetings {
func displayMessage() -> String {
return “Welcome !!!”
}
}
[/code]

The above class has a method named displayMessage that returns String. The access level for both Greetings class and the method is set to internal (default access level). Hence users will be able to access this class and function with in the module.

Let us replace viewDidLoad method in ViewController.swift with the following code snippet.

[code language=”swift”]
override func viewDidLoad() {
super.viewDidLoad()

let greetings = Greetings()
println(greetings.displayMessage())

}
[/code]

You will be able to access Greetings class as well as displayMessage() function. Now if you change the access level for displayMessage() to private then you should see an error message.

[code language=”swift”]
private func displayMessage() -> String {
return “Welcome !!!”
}[/code]

You can define a member type as private when it should be available within the source file (here it is Greetings class).

Add Second Module

Create a new framework within the AccessControlDemo project called RSModule and add new swift file with name as StringExtras and followinng implementation.

[code language=”swift”]public class StringExtras {
public static func makeFirstCharacterUpperCasse(word: String) -> String {
return word.capitalizedString
}
}[/code]

Note that the access level for class and function is set to public as we want to make these members available outside RSModule framework. Also the scope of the function is set to be static as we want to make class level funciton.

Add and import framework

Now to access StringExtras class inside Greetings.swift file, we need to Add and import RSModule to AccessControlDemo project. You can make RSModule available to this project by incuding this as part of the Target Dependcies. Click AccessControlDemo target, navigate to Build Phases and pick RSModule framework.

Navigate to Greetings.swift file, import the framework by adding import RSModule at the begining of the class and call the funciton in StringExtras class which capitalizes the first letter.

[code language=”swift”]
import RSModule

class Greetings {
func displayMessage() -> String {
return StringExtras.makeFirstCharacterUpperCasse(“welcome !!!”)
}
}[/code]

Download the source code from here.


Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.