ios swift:声明公共变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28191815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:33:45  来源:igfitidea点击:

swift: declare public variable

iosxcodevariablesswiftpublic

提问by rishu1992

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

我想将uploadURL设为public,即在其他类中赋值。当我在var uploadURL:String!它前面添加 public 时,建议我将其设置为内部。我要公开。请帮忙

回答by Antonio

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.

如果您的项目仅包含一个应用程序,那么您可能不需要 public - internal 具有相同的效果。如果您正在开发一个框架,并且需要从其他模块中的代码访问该属性,那么您需要将整个类和公开的方法/属性声明为公共。

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.

当您编写一个简单的单目标应用程序时,您应用程序中的代码通常在应用程序中是自包含的,不需要在应用程序模块之外提供。internal 的默认访问级别已经符合此要求。因此,您无需指定自定义访问级别。但是,您可能希望将代码的某些部分标记为私有,以便对应用程序模块中的其他代码隐藏它们的实现细节。

回答by Dániel Nagy

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

如果包含它的类也是公开的,则可以将其设为公开,因此请根据它进行更改。

回答by Muhammad Waqas Bhati

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

只需在开始时添加一个关键字“public”,这将使其在应用程序中公开。