Search code examples
swiftprivateswift4access-control

What is the significance of filePrivate now in Swift 4?


Since now "Private" can be accessed within an extension what is the significance of "file private". Can anyone explain with an example.


Solution

  • 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
        }
    }