在 Swift3 中声明全局变量/常量的私有与文件私有? [英] private vs. fileprivate on declaring global variables/consts in Swift3?
问题描述
我应该使用 private 还是 fileprivate 在 Swift 3 中声明全局变量/常量?例如
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
}
或者
private let a = 1
private class someClass {
fileprivate b = 0
}
推荐答案
在文件层面真的没有区别,无论你使用fileprivate
的private
,访问控制都会相同,例如以这种方式定义的常量将仅在该文件中可用.
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.
在这种情况下,对于所有意图和目的,您应该使用 fileprivate
,因为它更清楚地说明了意图 -> 您想在整个文件中使用它.
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.
在 swift 的未来版本中期望(在 swift 4 中有一些提议的更改)是非常合理的,这些修饰符确实会发生变化,fileprivate
和 private
在这种情况下将具有不同的范围.
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.
这篇关于在 Swift3 中声明全局变量/常量的私有与文件私有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!