rishu1992
rishu1992

Reputation: 1434

swift: declare public variable

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

Upvotes: 12

Views: 31280

Answers (4)

K_Mohit
K_Mohit

Reputation: 528

Very neatly explained on docs.swift.org

enter image description here

Upvotes: 0

Antonio
Antonio

Reputation: 72760

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.

Upvotes: 17

Muhammad Waqas Bhati
Muhammad Waqas Bhati

Reputation: 2805

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

Upvotes: 2

Dániel Nagy
Dániel Nagy

Reputation: 12015

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

Upvotes: 4

Related Questions