• Skip to main content
  • Skip to primary sidebar

Ravi Shankar

Tweaking Apps

  • About
  • Portfolio
  • Privacy Policy

Access Control

Access Control in Swift

July 20, 2015 By Ravi Shankar Leave a Comment

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

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.

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.

Filed Under: Programming, Xcode Tagged With: Access Control, Target Dependencies

Primary Sidebar

Recent Posts

  • We have blocked all requests from this device – Firebase Phone Authentication
  • iPhone is not available error message in Xcode
  • Clear CocoaPods cache, re-download and reinstall all pods
  • PDFKit – View, Annotate PDF file in Swift
  • Tab Bar Controller with WebView

Archives

  • September 2020
  • April 2020
  • December 2019
  • November 2019
  • October 2019
  • February 2019
  • October 2017
  • June 2017
  • May 2017
  • March 2017
  • September 2016
  • March 2016
  • February 2016
  • January 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • November 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • April 2011
  • March 2011
  • January 2011
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • July 2009
  • March 2008

Copyright 2020 © rshankar.com