本文介绍了私人与fileprivate在Swift3中声明全局变量/常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该使用private还是fileprivate在Swift 3中声明全局变量/常量?
eg

  

它在文件级别上确实没有区别,无论您使用 private fileprivate ,访问控制将是相同的,例如用这种方式定义的常量只能在该文件中使用。



对于其他修饰语也是如此,在某些情况下,内部和私人变得相同,例如。相同的单个文件模块。



对于这种情况下的所有意图和目的,您应该使用 fileprivate ,因为它更清晰说明意图 - >你想在整个文件中使用它。



在未来版本的swift中期望是合理的(在swift中有一些提议的修改) ,这些修饰符确实会发生变化,在这种情况下, fileprivate 和 private 将具有不同的作用域。

Should I use private or fileprivate to declare global variables/consts in Swift 3? e.g.

fileprivate let a = 1
fileprivate class SomeClass {
    fileprivate b = 0
}

Or

private let a = 1
private class someClass {
    fileprivate b = 0
}
解决方案

It really makes no difference at file level, whether you use private of fileprivate, access control will be the same, for example constants defined this way will be only usable in that file.

Same can be said for other modifiers, in certain cases, internal and private become the same, eg. same single file module.

For all intents and purposes in this case you should use fileprivate, because it more clearly states the intent -> you want to use this in whole file.

It is quite reasonable to expect in future versions of swift (there are some proposed changes in swift 4), that there will indeed be change in these modifiers, and fileprivate and private will have different scope in this case.

这篇关于私人与fileprivate在Swift3中声明全局变量/常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 12:38