Since now "Private" can be accessed within an extension what is the significance of "file private". Can anyone explain with an example.
Asked
Active
Viewed 1,267 times
6
-
3`private` is private within just that class within that file (e.g. accessible to extensions within that same file). `fileprivate` is private across classes in that file. – Rob Jan 20 '18 at 07:46
-
okay so basically "file private" is accessible to anything and everything throughout the source file whereas "private" is applicable only for the class in which it is defined and in extensions of that class. – subin272 Jan 20 '18 at 07:54
-
Possible duplicate of [What is a good example to differentiate between fileprivate and private in Swift3](https://stackoverflow.com/questions/39027250/what-is-a-good-example-to-differentiate-between-fileprivate-and-private-in-swift) – Hamish Jan 20 '18 at 10:28
-
A practical use case of `fileprivate` may be when you create a custom "thing", like a menu, that has a number of different custom objects (`NSObject`, `UIButton`, etc.) to make it work, and you want those objects to work together but you don't want them exposed to everything else (just the menu). To achieve that, you would create the menu with all of those objects in one file and make use of `fileprivate`. – trndjc Jan 21 '18 at 18:33
1 Answers
13
private
limits access to that class within that file. fileprivate
limits access to that file.
Imagine these are all in the same file:
class Foo {
private var x = 0
fileprivate var y = 0
}
extension Foo {
func bar() {
// can access both x and y
}
}
class Baz {
func qux() {
let foo = Foo()
// can access foo.y, but not foo.x
}
}

Rob
- 415,655
- 72
- 787
- 1,044