我已经阅读了 Apple 提供的文档,即 https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html
在这里,它解释了:
Classes with public access, or any more restrictive access level, can be subclassed only within the module where they’re defined.
Internal access enables entities to be used within any source file from their defining module, but not in any source file outside of that module. You typically use internal access when defining an app’s or a framework’s internal structure.
Open classes can be subclassed within the module where they’re defined, and within any module that imports the module where they’re defined
我没有发现公共(public)和内部访问之间的确切区别。
我的研究包括 http://www.globalnerdy.com/2014/07/23/a-first-look-at-swifts-new-access-levels/以及。但是根据 swift3.3 文档,它并不清楚。
最佳答案
Swift 的访问控制模型是基于模块
和源文件的概念。
模块是代码分发的单个单元——一个框架或应用程序作为一个单元构建和交付,并且可以由另一个模块使用 Swift 的 import
关键字导入。
例如一些流行的模块:Alamofire、SwiftyJson、RxSwift ...
您要导入到项目中的这些模块。所有使用 internal access level
的类只能在这些库/模块中使用。您不能直接在代码中使用它们。
但是如果该类是 public
- 您可以直接在您的源代码中使用它。
关于ios - 公共(public)与内部访问修饰符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44453937/