Swift 4 中现在的 filePrivate 有什么意义?
What is the significance of filePrivate now in Swift 4?
自从现在 "Private" 可以在扩展中访问 "file private" 的意义是什么。谁能举例说明一下。
private
限制对该文件中 class 的访问。 fileprivate
限制对该文件的访问。
假设这些都在同一个文件中:
class Foo {
private var x = 0
fileprivate var y = 0
}
extension Foo {
func bar() {
// can access both x and y
}
}
class Baz {
func qux() {
let foo = Foo()
// can access foo.y, but not foo.x
}
}
自从现在 "Private" 可以在扩展中访问 "file private" 的意义是什么。谁能举例说明一下。
private
限制对该文件中 class 的访问。 fileprivate
限制对该文件的访问。
假设这些都在同一个文件中:
class Foo {
private var x = 0
fileprivate var y = 0
}
extension Foo {
func bar() {
// can access both x and y
}
}
class Baz {
func qux() {
let foo = Foo()
// can access foo.y, but not foo.x
}
}