swift: declare public variable
Asked Answered
H

4

12
class XYActivity: UIActivity,YouTubeHelperDelegate
{
    var youTubeHelper:YouTubeHelper
    var uploadURL: String!
    override init() {
        self.youTubeHelper = YouTubeHelper()
    }
    override  func activityType() -> String? {
        return nil
    }
//
}

I want to make uploadURL public, That is, to be assigned in other class. When I add public infront of var uploadURL:String! it suggest me to make it as internal. I wanna make it public. Please help

Honniball answered 28/1, 2015 at 12:2 Comment(0)
A
17

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.

Anastatius answered 28/1, 2015 at 12:7 Comment(2)
so I must mention public or private for all methods ? It ask me to do so if I add public infront of class declarationHonniball
If you don't specify a modifier, the default is internal. You use private to limit visibility of an entity to the current file where it is defined. You use public instead to make it available to any module. I suggest reading the documentation, I posted the link in the answer.Anastatius
K
4

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

Kirovograd answered 28/1, 2015 at 12:7 Comment(1)
Yes, exactly like that.Valentinvalentina
F
2

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

Facilitation answered 28/1, 2015 at 12:8 Comment(0)
S
0

Very neatly explained on docs.swift.org

enter image description here

Stefansson answered 24/6, 2022 at 14:42 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.