It seems I can declare variables like below in a source file:
private let var1 = 1234
fileprivate let var2 = "abcd"
class MyClass {
// ...
}
So what's the difference between the 2 vars here?
Consider this:
private enum E1 {
static private let e1x = "x"
static fileprivate let e1y = "x"
}
fileprivate enum E2 {
static private let e2x = "x"
static fileprivate let e2y = "x"
}
func test() {
print(E1.self)
print(E2.self)
print(E1.e1x)
// ^ 'e1x' is inaccessible due to 'private' protection level
print(E1.e1y)
print(E2.e2x)
// ^ 'e2x' is inaccessible due to 'private' protection level
print(E2.e2y)
}
At the top level — that is, E1
and E2
— the private
and fileprivate
modifiers are the same. However, once you start nesting (e1x
, e1y
, e2x
, and e2y
) you see the difference:
-
private
stuff is only available within the enclosing scope. -
fileprivate
stuff is available anywhere within the file.
IMPORTANT Please reply as a reply, not in the comments. If you reply in the comments, I won't be notified. For this and other tips about using DevForums effectively, see Quinn’s Top Ten DevForums Tips.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"