Search code examples
iosswift4.2access-specifier

Private class member accessible outside class


Why are we allowed to assign public access specifier for a member in a private class i.e. incorrectVariable in the code below:

My code doesn't give compilation error and run properly, my code is:

private class C {
    public var incorrectVariable = "SomeString"
    var a = 5
    func fooFun() -> Int {
        self.a += 1
        return self.a
    }
}

var obj = C().a
print(obj)
obj = C().fooFun()
print(obj)

Solution

  • If you're creating private class object with same file there is not an issue. Private class not accessible in other file.

    Refer this access control for detail link