Private var is accessible from outside the class

Access modifiers in Swift are implemented differently than other languages. There are three levels:

private: accessible only within that particular file

internal: accessible only within the module (project)

public: accessible from anywhere

Unless marked otherwise, everything you write is internal by default.

The Swift blog had a post about access control when the features were introduced in beta 4, and Apple’s documentation has a chapter as well.

Leave a Comment