访问控制/Access Control
Full access control annotation in tutorials can distract from the main topic and is not required. Using private
and fileprivate
appropriately, however, adds clarity and promotes encapsulation. Prefer private
to fileprivate
; use fileprivate
only when the compiler insists.
在教程中,完整的访问控制注释会偏离主题且是不必要的。然而,适时地使用 private
和 fileprivate
会使代码更加清晰,也会有助于封装。 在合理情况下,private
要优于 fileprivate
。 只有编译器强制的情况下使用 fileprivate
。
Only explicitly use open
, public
, and internal
when you require a full access control specification.
只有需要完整的访问控制格式时,才显式地使用 open
、public
和 internal
。
Use access control as the leading property specifier. The only things that should come before access control are the static
specifier or attributes such as @IBAction
, @IBOutlet
and @discardableResult
.
把访问控制相关的关键字用作前置属性说明符。仅有 static
说明符或诸如 @IBAction
、@IBOutlet
和 @discardableResult
标志应该放在访问控制符前面。
推荐(Preferred):
private let message = "Great Scott!"
class TimeMachine {
private dynamic lazy var fluxCapacitor = FluxCapacitor()
}
不推荐(Not Preferred):
fileprivate let message = "Great Scott!"
class TimeMachine {
lazy dynamic private var fluxCapacitor = FluxCapacitor()
}