I'm confused about the internal and private access modifier.
The docs says:
“Internal access enables entities to be used within any source file from their defining module, but not in any source file outside of that module. You typically use internal access when defining an app’s or a framework’s internal structure.”
How I thought it was, was that with internal you can access everything if you are in your own app. But this is not true, because when I have a viewcontroller what is default internal and I'm having a internal function on that viewcontroller I can't access this from another file in another group (You create these in xCode).
What I tried was having a ViewController that has a method foo in group A then in group B I created a ViewController like this:
let vc: FakeViewController = FakeViewController() vc.foo()
So is internal restricted to the same group? Or I'm I interpreting it wrong?
Is it useful that in a viewcontroller you create private methods and vars/lets?
You typically use internal access when defining an app's or a framework's internal structure. File-private access restricts the use of an entity to its own defining source file.
Internal - This is default access specifier in swift. With this we can access data members and member functions in the same module (target). Public - This is where you can access all data members and member functions within same module and outside of it. But you can't subclass or override outside the module.
fileprivate means an entity that is accessible anywhere in that file. private means an entity that cannot be accessed anywhere except for the enclosing type, such as a class.
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.
@user1007522 Could you post the entire source code for FakeViewController? You should have access to foo() from your vc variable. If you do not, I suspect something else is in play here.
I found the following definitions much easier to understand (copied from UseYourLoaf - Swift 4 Access Levels)
The Five Access Levels of Swift 3/4
Swift 3 has five access levels that control from which source file or module you can access something. In order from most open to most restricted:
open
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 that module.
public
allows the same access as open - any source file in any module - but has more restrictive subclassing and overriding. You can only subclass a public class within the same module. A public class member can only be overriden by subclasses in the same module. This is important if you are writing a framework. If you want a user of that framework to be able to subclass a class or override a method you must make it open.
internal
allows use from any source file in the defining module but not from outside that module. This is generally the default access level.
fileprivate
allows use only within the defining source file.
private
Swift 4: allows use only from the enclosing declaration and new in Swift 4, to any extensions of that declaration in the same source file Swift 3: allows use only from the enclosing declaration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With