Swift 拾遗 - 访问控制
source link: https://kingcos.me/posts/2016/swift_access_control/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Swift 拾遗 - 访问控制
2021.05.24 by kingcosPreface · 序
《Swift 拾遗》是一个关于 Swift 的新文章专辑,这个系列的文章将不会涉及基本语法的层面,而是尝试从底层「拾」起之前所忽视的内容。今天我们将一起简单探究 Swift 中的访问控制。
限制等级
跨模块*访问
跨文件访问
细节
open
🉑️
🉑️
1. 仅可修饰类与类的成员变量
2. 修饰的类可被跨模块继承,但默认内部成员仍为 internal
public
🉑️
🉑️
修饰的类不可被跨模块继承
internal
❌
🉑️
默认限制等级
fileprivate
❌
❌
仅限同一文件内访问
private
❌
❌
仅限同一作用域内访问({}
)
Q:什么是模块*?
A:模块,即 Module:
A module is a single unit of code distribution—a framework or application that’s built and shipped as a single unit and that can be imported by another module with Swift’s import keyword.
根据 Apple 官方所述:模块是一个单一的代码分发单元——一个作为单一单元被构建和运送的框架或应用程序,并且可以通过 Swift 的 import 关键字被另一个模块导入。也就是说,我们通过新建 Target 所引入的 Library / Framework,或者通过 CocoaPods 引入的第三方库,均属于一个模块。
Reference
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK