Difference between private and fileprivate?

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?

Answered by DTS Engineer in 794313022

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"

You'll get this type of information in Swift programming language.

fileprivate

Apply this modifier to a declaration to indicate the declaration can be accessed only by code in the same source file as the declaration.

private

Apply this modifier to a declaration to indicate the declaration can be accessed only by code within the declaration's immediate enclosing scope.

For a more comprehensive explanation with examples: https://www.avanderlee.com/swift/fileprivate-private-differences-explained/

I personally prefer using private.

Accepted Answer

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"

Difference between private and fileprivate?
 
 
Q