Private var is accessible from outside the class

前端 未结 2 565
感情败类
感情败类 2020-12-09 14:52

This was done in Playground, just to simplify.

class MyPrivateVar
{
    private var priv: String?
}

var myInstance = MyPrivateVar()

myInstance.priv = \"Som         


        
2条回答
  •  感情败类
    2020-12-09 15:40

    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.

提交回复
热议问题