Swift Access Levels. fileprivate?

Bob Godwin
3 min readApr 1, 2017

--

Swift 3.0 came with six access level.

  1. open
  2. public
  3. Internal
  4. private
  5. fileprivate ?
  6. final

Open means that you can access open classes and class members from any source file in the defining module or any module that imports that module. You can subclass an open class or override an open class member both within their defining module and any module that imports the class.

Public is literally the same thing as open but with a subtle difference. You can only subclass a class or ovverride it’s function only from the same module. Now before we go head let’s try to understand what is a module? So your app definitely uses UIKit for example. So your app is a module that uses a module called UIKit. So in terms of public you can only subclass or ovverride functions from UIKit from UIKit! Not from your app. No panic UIKit has it all as open.

Internal keyword is what has been confusing swift new comers. Especially those coming from Java and C++. The swift internal is default access and allows use of a function or property from any source file within the defining module but not from outside the module. So all function and properties within your app that not marked with a compiler keyword is by default marked as internal.

Private is pretty simple and it allows the use of function and properties only from the enclosing declaration. Which when translated makes more sense because it keeps everything at scope level. If you want to keep some implementation details away from the public you can mark it with the private keyword.

Fileprivate this keyword is unsual. First let’s try to understand clearly what this mean. fileprivate allows use of a function or property only within the defining source file. Hmmm what a confusing keyword. So the swift compiler says that the property is NOT private and that it is internal BUT you can only use it if you find your self within the same file! Damm! A better explanation is this. Just immagine a car sharing company telling you that the car can be driven by everyone. But you can only drive the car in one street. So if you find your self in the same street you can drive the car if not you can’t drive the car. So my question is why be a car sharing in the first place?. Another example is like saying everyone can eat pasta but only the Italians have the right to cook pasta. The fileprivate enforces a one class per file structure, which is very limiting and secondly you lose the potentials of extensions. Swift main purpose is to empower developers and not to limit them. fileprivate defines a cumbersome, limiting, confusing and unnecessary access lock on file level instead of scope level. My personal advice is to avoid it and use private. However there is already a proposal to change this awkward behavior in SE-159

Final. the final keyword is not an access level but you can add it to any of the access levels other than open to prevent subclassing or overriding. There is a potential performance improvement from this as the compiler can avoid dynamic dispatch though you can allow the compiler to infer this if you use whole module optimization

Please feel free to reach out on twitter, thanks for reading.

Other Articles:

--

--