Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 580
subin272 Avatar asked Jan 20 '18 07:01

subin272


1 Answers

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
    }
}
like image 133
Rob Avatar answered Oct 15 '22 12:10

Rob