I'm starting in swift and opening a project created using swift2 from xcode 8 beta, the private
modifier were changed to fileprivate
. what does this keyword means? and how is different from private
?
In Swift: 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.
If you mark something fileprivate it can be read anywhere in the same file it was declared – even outside the type. On the other hand, a private property can only be read inside the type that declared it, or inside extensions to that type that were created in the same file.
private is now accessible in extension but within same file. If you declare/define extension in other file, then your private variable will not be accessible to your extension. fileprivate is accessible within same file.
An open class member is accessible and overridable outside of the defining module. A public class is accessible but not subclassable outside of the defining module. A public class member is accessible but not overridable outside of the defining module.
fileprivate
is one of the new Swift 3 access modifiers that replaces private
in its meaning. fileprivate
defines an entity (class, extension, property, ...) as private to everybody outside the source file it is declared in, but accessible to all entities in that source file.
private
restricts the entity in the direct enclosing scope.
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