Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'fileprivate' keyword means in Swift? [duplicate]

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 ?

like image 442
Nathaniel Avatar asked Sep 16 '16 14:09

Nathaniel


People also ask

What does Fileprivate mean in Swift?

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.

What is difference between private and Fileprivate?

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.

Can we use Fileprivate in extension?

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.

What is the difference between open and public keywords in Swift?

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.


1 Answers

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.

like image 112
Jans Avatar answered Oct 19 '22 08:10

Jans