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.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...