ios – swift:声明公共变量

前端之家收集整理的这篇文章主要介绍了ios – swift:声明公共变量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
class XYActivity: UIActivity,YouTubeHelperDelegate
{
    var youTubeHelper:YouTubeHelper
    var uploadURL: String!
    override init() {
        self.youTubeHelper = YouTubeHelper()
    }
    override  func activityType() -> String? {
        return nil
    }
//
}

我想让uploadURL公开,也就是说,要在其他类中分配.
当我添加公共infront的var uploadURL:String!它建议我把它作为内部.我想让它公开.请帮忙

解决方法

为了使其公开,该类必须被公开.

默认情况下,修饰符是内部的,这使得当前模块中的任何地方的类,方法属性未被显式声明为私有.

如果你的项目仅由一个应用程序组成,那么你可能不需要公共 – 内部的效果相同.如果您正在开发框架,并且需要该属性可以从其他模块中的代码访问,则需要将整个类和暴露的方法/属性声明为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相关文章