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
?
Asked
Active
Viewed 2.9k times
65

Nathaniel
- 952
- 1
- 7
- 12
1 Answers
125
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.
-
7what do you mean by "direct enclosing scope"? When would I use just `private` instead of `fileprivate`? – rolling_codes Oct 10 '16 at 19:45
-
4the enclosing scope is the set of blocks or file in this extend in which an entity is declared, `private` is meant to hide implementation details or keep things hidden that are restricted to the owner while `fileprivate` can be used to share some common functionality between all entities in the same file. – Jans Oct 10 '16 at 20:47
-
4In swift < 3.0 'private' works like 'fileprivate' in swift 3.0. If you declare in swift 2.3 e.g. two classes with private variables but in the same file they had still access to each of 'private' marked variables. – Robert Oct 16 '16 at 19:33