Swift 3 access of private properties in a struct from a extension Swift 3 access of private properties in a struct from a extension swift swift

Swift 3 access of private properties in a struct from a extension


From the Swift's book:

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

So a private variable is only accessible within the set of curly brackets where it's defined. Swift's Access Control has never been like those of C++, Java or C#.


With release of Swift 4 this has been changed if the extension is implemented within the same file. You can refer the doc herePrivate Member in Extension

Example below is from Swift 4

protocol SomeProtocol {    func doSomething()}struct SomeStruct {    private var privateVariable = 12}extension SomeStruct: SomeProtocol {    func doSomething() {        print(privateVariable)    }}