gpt4 book ai didi

swift3 - 文件私有(private)和私有(private)扩展之间的区别?

转载 作者:行者123 更新时间:2023-12-04 10:23:13 26 4
gpt4 key购买 nike

swift 3.0

我知道fileprivate访问级别修饰符将函数/属性的使用限制在声明它的源文件和 private - 仅限于声明的词法范围。但似乎这条规则不适用于扩展。例如。此代码有效:

class Foo {
}

fileprivate extension Foo {
var aa: Int {
return aaa + 10
}
}

private extension Foo {
var aaa: Int {
return 20
}
}

有人可以帮我弄清楚它们之间的区别吗?谢谢。

swift 4.0
private现在可以在扩展名中但在同一个文件中访问。如果您在其他文件中声明/定义扩展名,则您的扩展名将无法访问您的私有(private)变量。
fileprivate可以在同一个文件中访问。

最佳答案

With Swift 4.0, scope of private and fileprivate is extended.
Private is now accessible within same file, outside actual class/extension. If you declare/define your extension in other file, then your private variable will not be accessible at that time fileprivate will work



文件私有(private)
文件私有(private)访问将实体的使用限制在其自己的定义源文件中。当在整个文件中使用这些细节时,使用文件私有(private)访问来隐藏特定功能的实现细节。
语法: fileprivate <var type> <variable name>示例: fileprivate class SomeFilePrivateClass {}
私有(private)
私有(private)访问将实体的使用限制为封闭声明,以及该 的扩展。同一文件中的声明 .当这些细节仅在单个声明中使用时,使用私有(private)访问来隐藏特定功能的实现细节。
语法: private <var type> <variable name>示例: private class SomePrivateClass {}
以下是有关所有访问级别的更多详细信息: Swift - Access Levels

关于swift3 - 文件私有(private)和私有(private)扩展之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43287220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com