swift: declare public variable swift: declare public variable xcode xcode

swift: declare public variable


In order to make it public, the class must be declared as public.

By default the modifier is internal, which makes classes, methods and properties not explicitly declared as private available anywhere in the current module.

If your project consists of an app only, then you probably don't need public - internal has the same effect. If you are developing a framework instead, and need that property accessible from code in other modules, then you need to declare the entire class and the exposed methods/properties as public.

Suggested reading: Access Control

Excerpt describing default access levels:

All entities in your code (with a few specific exceptions, as described later in this chapter) have a default access level of internal if you do not specify an explicit access level yourself. As a result, in many cases you do not need to specify an explicit access level in your code.

and access levels for single-target apps:

When you write a simple single-target app, the code in your app is typically self-contained within the app and does not need to be made available outside of the app’s module. The default access level of internal already matches this requirement. Therefore, you do not need to specify a custom access level. You may, however, want to mark some parts of your code as private in order to hide their implementation details from other code within the app’s module.


You can make it public if the class that contains it is also public, so change that according to it.


JuST ADD a keyword "public" at start this will make it public in the app.