swift:声明public变量
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
}
//
}
我想把uploadURL做成public,也就是要赋值在otherclass。
当我在 var uploadURL:String!
前面添加 public 时,它建议我将其设为内部。我想做到 public。请帮助
如果包含它的 class 也是 public,则可以将其设为 public,因此请根据它进行更改。
为了使其成为 public,必须将 class 声明为 public。
默认情况下,修饰符是内部的,这使得 class 没有明确声明为私有的方法和属性在当前模块的任何地方都可用。
如果您的项目仅包含一个应用程序,那么您可能不需要 public - 内部具有相同的效果。如果您正在开发一个框架,并且需要从其他模块中的代码访问 属性,那么您需要将整个 class 和公开的 methods/properties 声明为 public。
建议阅读:Access Control
描述默认访问级别的摘录:
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.
和单一目标应用程序的访问级别:
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.
只需在开始时添加一个关键字 "public" 这将使它在应用程序中成为 public。
class XYActivity: UIActivity,YouTubeHelperDelegate
{
var youTubeHelper:YouTubeHelper
var uploadURL: String!
override init() {
self.youTubeHelper = YouTubeHelper()
}
override func activityType() -> String? {
return nil
}
//
}
我想把uploadURL做成public,也就是要赋值在otherclass。
当我在 var uploadURL:String!
前面添加 public 时,它建议我将其设为内部。我想做到 public。请帮助
如果包含它的 class 也是 public,则可以将其设为 public,因此请根据它进行更改。
为了使其成为 public,必须将 class 声明为 public。
默认情况下,修饰符是内部的,这使得 class 没有明确声明为私有的方法和属性在当前模块的任何地方都可用。
如果您的项目仅包含一个应用程序,那么您可能不需要 public - 内部具有相同的效果。如果您正在开发一个框架,并且需要从其他模块中的代码访问 属性,那么您需要将整个 class 和公开的 methods/properties 声明为 public。
建议阅读:Access Control
描述默认访问级别的摘录:
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.
和单一目标应用程序的访问级别:
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.
只需在开始时添加一个关键字 "public" 这将使它在应用程序中成为 public。