The keywords “public” “private” and “static” and others appear quite often in example code, answers on Stack Overflow, and projects on GitHub. What are they and what do they mean?
What is Access Control?
“[Access Control] gives you complete control over what part of the code is accessible within a single file, available across your project, or made public as API for anyone that imports your framework.” — Apple development blog
“Access control lets you specify what data inside structs and classes should be exposed to the outside world” — Hacking with Swift
Access control can be applied to classes and structs and the properties, functions, enums, and types they contain.
Great resource on learning about Access Control: A first look at Swift’s new access levels (quoted below).
Four Levels of Access Control in Swift
- 1. open and public: restricted to any source file within the defining module or an external file that imports the defining module.
publicentity can be “seen” within the file where it’s defined, from any other file in the same application or framework, and by another file any other application or framework that imports the application or framework where the
publicentity was defined.”
- 2. internal (default): restricted to any source file within the defining module.
internalentity can be “seen” within the file where it’s defined, as well as from any other file in the same application or framework.”
- 3. fileprivate: restricted to its own defining source file.
- 4. private: restricted to the enclosing declaration.
privateentity can be “seen” within the file where it’s defined, and only within that file. It’s invisible from outside that file.”
Are static properties, methods, and functions part of access control? Well, it seems that using the “static” keyword is a little different from access control. The “static” keyword restricts restricts access of a function or property to the enclosing class…………….
What is the difference between a “static” function and a “private” function? You would use “static” when the you don’t need an instance of the function. This is generally helpful for general or repetative work where you don’t need to maintain the data/state between calls.
let greeting = Greeting()print(greeting.displayGreeting()
You would use “private” when you simply want to hide the function from other code. A function can be static, private, neither, or both.
Here are other good examples/explanations:
Static modifier is used to create variables and methods that will exist independently of any instance created for the class. Static members exists before any instance of the class is created.
Also there will be only one copy of the static member.
To call a static method displayRuns() of the class named Cricket we write
Class name is used to invoke the static method as static member does not depend on any instance of the class.
private static method means you can not invoke the method from outside the class as the method is private to the class. — Code Ranch
“Static vs. non-static (a.k.a. ‘instance methods’) indicates whether the method operates on the class itself (static) or on one particular instance (non-static).” “Use static methods to implement behavior that is conceptually linked to the class, but does not ‘bind’ to one particular instance.”— tdammers
This is the error you get when you try to access a private function from a different file:
function_name' is inaccessible due to 'private' protection level. This is the error you get when you try to access a static function from a different file:
Static member 'function_name' cannot be used on instance of type 'type_name'.