1

As the sample code here, what's the difference of private and fileprivate class in the same file in Swift?

//  ViewController.swift

private class A {
    var name: String = ""

    let d = B.D()
}

fileprivate class B {
    var name: String = ""

    private let c = C()
    private class C {
        var number: String = ""
    }

    fileprivate let d = D()
    fileprivate class D {
        var number: String = ""
    }
}

Since class A and B are in the sample file, A can't be any more private then B, right?

Sorry this is a very picky question, but I just want to confirm it.

allenlinli
  • 2,066
  • 3
  • 27
  • 49

2 Answers2

3

Access modifiers in swift have been charged a few times during the past few releases. For now, let me suggest a code sample instead of explanation first:

All code in one file:

fileprivate class Foo {

  private var someVariablePrivate: Int?
  fileprivate var someVariableFileprivate: Int?

  private func makeBar() { }
  fileprivate func makeFoo() { }
}

extension Foo {

  func bar() {
    let value = someVariablePrivate // OK
    let value2 = someVariableFileprivate // OK
  }
}

private class Foofoo: Foo {

  override func makeFoo() {
    // OK
  }

  override func makeBar() {
    // NOK, because private
  }
}

class Bar {

  func foo() {
    let object = Foo()
    let value = object.someVariableFileprivate // OK
    let value2 = object.someVariablePrivate // NOK - because it private to that class
  }
}

Previously, private restrict access only to type definition, but within few lates version of Swift, it has been modified and updated, so even extension of same type can use private variables

As Apple mention:

File-private access restricts the use of an entity to its own defining source file. Use file-private access to hide the implementation details of a specific piece of functionality when those details are used within an entire file.

Private access restricts the use of an entity to the enclosing declaration, and to extensions of that declaration that are in the same file. Use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration.

we could compare a bit this modifiers:

enter image description here

why do we ned both of them:

You may, however, want to mark some parts of your code as file private or private in order to hide their implementation details from other code within the app’s module.

hbk
  • 10,908
  • 11
  • 91
  • 124
  • Very detailed comparisons! There are two situations that I am not quite sure. I think they should be: extension in same file: x / x inherited objects: x / x Overall, it seems like there's no difference in my case. – allenlinli Jul 10 '20 at 09:26
  • 1
    tricky moment extension in same file i mean for different types :); but if we are thinking about one type - than u are right – hbk Jul 10 '20 at 10:49
0

When to use fileprivate

Although the keywords are almost the same, there is a clear difference in their use cases. Fileprivate access restricts the use of an entity within the same defined source file. The only reason you would use fileprivate is when you want to access your code within the same file from different classes or structs.

When to use private

The private keyword is used a lot more and restricts the use of an entity to the enclosing declaration and its extensions. The extensions, however, have to be defined within the same file. In other words, private declarations will not be visible outside the file. You can use this keyword to only expose the minimal code needed to interact with the entity. This will improve readability and makes it easier to use and understand your code for others.

your question: Since class A and B are in the sample file, A can't be any more private then B, right?

In case of class A with private access specifier it won't be accessible outside it's declared scope.

In case of class B it will be accessible to the class A but it's private properties won't be accessible in class A

For more information, you can refer to this interesting article:

https://www.avanderlee.com/swift/fileprivate-private-differences-explained/#when-to-use-private

Jarvis The Avenger
  • 2,750
  • 1
  • 19
  • 37
  • The explanation seems a bit off. The question is about the class `A` itself, not about the properties inside `A`. But thank you for the reference still. – allenlinli Jul 10 '20 at 09:05