Swift 3 中Private和Fileprivate的区别
来源:互联网 发布:ubuntu 升级python 编辑:程序博客网 时间:2024/05/23 19:35
当Swift首次引入访问级别时,对此进行了一些混淆和不解。虽然开发人员对于添加对Swift编程语言的访问控制感到兴奋,但是
private
关键字的行为与其他编程语言的行为不同。这在Swift 3中已经发生了改变,通过添加另一个关键字进行私有访问控制
fileprivate
。区别是微妙但容易理解。Swift 3 之前
在引入Swift 3之前,private关键字将实体(类,结构,枚举,...)的使用限制在定义它们的源文件中。看下面的例子。
import UIKitclass NotesViewController: UIViewController { private var dataSource = [String]()}extension NotesViewController: UITableViewDataSource { func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return dataSource.count } ...}
我们声明一个
UIViewController
子类NotesViewController
, 他带有一个[String]
类型的私有属性dataSource
。在同一个源文件中,我们为NotesViewController
类创建一个扩展,遵守UITableViewDataSource
协议。有趣的是我们可以访问扩展中的dataSource
属性(dataSource.count
),即使属性被声明为private
。Swift3中
如果我们将上述代码片段移植到
Swift 3
,编译器会抛出一个错误。它告诉我们dataSource
属性是一个未解析的标识符。换句话说,通过将dataSource
属性声明为private
,扩展名中不可访问。两个不同的Private:Private 和 filePrivate
Private
您仍然可以使用私人关键字进行访问控制。 Swift团队已经听取了社群的反馈意见,因此,私人关键字的含义现在与许多其他编程语言相似。声明为私有的实体只能在其声明的范围内被访问。
import UIKitclass NotesViewController: UIViewController { private var dataSource = [String]()}extension NotesViewController: UITableViewDataSource { func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return dataSource.count } ...}
filePrivate
Swift 3引入了fileprivate关键字来替换原来的private关键字。您可以试着将dataSource属性的访问权限标记为fileprivate。
import UIKitclass NotesViewController: UIViewController { fileprivate var dataSource = [String]()}extension NotesViewController: UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return dataSource.count } ...}
顾名思义,
fileprivate
关键字将对实体的访问权限于它声明的源文件。我们可以通过将NotesViewController
类的扩展名移动到单独的文件来验证这一点。这导致我们之前遇到的错误。这样修改的好处
private
和filePrivate
刚开始可能会令人困惑,但是private
拥有更清晰、更直观的语意。private
的原始实现使许多开发人员,特别是来自其他编程语言的开发人员感到困惑。通过将private
重命名为fileprivate
,不再如此。该名称清楚地表示一个实体对于特定(源)文件是私有的,而现在的private
和其他的编程语言里的private
有相同的语意,更加清晰。
作者:kevinLY
链接:http://www.jianshu.com/p/030995b4beba
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
- Swift 3 中Private和Fileprivate的区别
- Swift-Private/FilePrivate/Public/Open的作用和区别
- private、fileprivate、public、open修饰符的作用和区别
- swift中访问控制(fileprivate,private,internal,public,open)
- Swift 3中新的访问控制关键字fileprivate和open
- Swift 3:新的访问控制fileprivate和open
- Swift 3必看:新的访问控制fileprivate和open
- Swift 3必看:新的访问控制fileprivate和open
- Swift 3必看:新的访问控制fileprivate和open
- Swift 3必看:新的访问控制fileprivate和open
- Swift 3必看:新的访问控制fileprivate和open
- Swift基础之:新的访问控制fileprivate和open
- Swift 新的访问控制fileprivate和open
- Swift - 访问控制(fileprivate,private,internal,public,open)
- Swift 4.0 中的 open,public,internal,fileprivate,private
- vb中DIM和PRIVATE的区别
- Swift中?和!的区别
- swift中 ? 和 ! 的区别
- 织梦联动地区后台显示为数字的解决办法
- 一个博客到底有多重要
- java通过jdbc对数据库的访问
- Java-Integer类
- 中国大数据人才标准专家研讨会在京举行
- Swift 3 中Private和Fileprivate的区别
- 【Memcached】libmemcached API使用
- SQL数据库面试题以及答案(50例题)
- Web--聊聊Web缓存机制
- 关于蓝牙mac地址规则
- 深入理解java中的组合和继承
- 【双11福利】文末评论免费送《零起点TensorFlow快速入门》
- 关于Spring中的<context:annotation-config/>配置
- Please change caller according to com.intellij.openapi.project.IndexNotReadyException documentation