gpt4 book ai didi

ios - swift:声明公共(public)变量

转载 作者:IT王子 更新时间:2023-10-29 05:26:01 25 4
gpt4 key购买 nike

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

我想把uploadURL设为public,也就是赋值给其他类。当我添加 var uploadURL:String! 的 public infront 时,它建议我将其设为内部。我想公开。请帮忙

最佳答案

为了使其公开,类必须声明为公开的。

默认情况下,修饰符是内部的,这使得未明确声明为私有(private)的类、方法和属性在当前模块的任何地方都可用。

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

关于ios - swift:声明公共(public)变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28191815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com