剑客
关注科技互联网

Access Control 笔记

Access control restricts(约束) access to parts of your code from code in other source files and modules.

You can assign specific access levels to individual types (classes, structures, and enumerations), as well as to properties, methods, initializers, and subscripts belonging to those types.

Protocols can be restricted to a certain context, as can global constants, variables, and functions.

Swift 提供默认的(隐式)权限控制层用于减少显式指定它,如果你的应用是一个 single-target 应用就完全不用显式指定权限控制层。

Modules and Source Files

Swift’s access control model is based on the concept of modules and source files.

Swift 的权限控制模型是基于 模块
源文件
的。

  • Each build target (such as an app bundle or framework) in Xcode is treated as a separate module in Swift.(每个构建目标都可以被认为是单独的模块)
  • A source file is a single Swift source code file within a module.(而单一的源文件可以包含多个定义的类型、方法等)

Access Levels

Swift 提供 5 种不同的权限控制层:(entities 是指 properties, types, functions, and so on)

  • Open access and public access enable entities to be used within any source file from their defining module, and also in a source file from another module that imports the defining module.(用于不同模块间的公开接口,它俩的区别在下面提及)

  • 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. (用于同一模块里的任意文件)

  • File-private access restricts the use of an entity to its own defining source file. (用于同一源文件里)

  • Private access restricts the use of an entity to the enclosing declaration. (用于单一声明里)

Open access is the highest (least restrictive) access level and private access is the lowest (most restrictive) access level.

Open 权限只能作用于类和类成员,它和 Public 权限的不同如下:

Public 或其他更高限制修饰的类只能在它定义的模块里子类化,修饰的类方法只能在它定义的模块里重载;而 Open 修饰的类可以在不同模块里子类化,修饰的类方法可以在不同模块里重载。

Guiding Principle of Access Levels

Access levels in Swift follow an overall guiding principle: No entity can be defined in terms of another entity that has a lower (more restrictive) access level.

Swift 权限控制遵从一个原则:没有实体可以在更高限制的实体里定义。

Default Access Levels

Swift 的默认控制控制层是 Internal(前面已经提到,它是控制在同一模块任意文件),所以单一目标就不同指定控制层,用默认的 Internal 就可以。

Access Levels for Frameworks

Any internal implementation details of your framework can still use the default access level of internal, or can be marked as private or file private if you want to hide them from other parts of the framework’s internal code. You need to mark an entity as open or public only if you want it to become part of your framework’s API.

Access Levels for Unit Test Targets

如果在 import 模块时用 @testable 属性修饰,单元测试模块就可以访问被导入的模块(默认 internal 权限),而不用修改被导入模块的权限控制。

Access Control Syntax

open, public, internal, fileprivate, 和 private 修饰词

public class SomePublicClass {}  
internal class SomeInternalClass {}  
fileprivate class SomeFilePrivateClass {}  
private class SomePrivateClass {}

public var somePublicVariable = 0  
internal let someInternalConstant = 0  
fileprivate func someFilePrivateFunction() {}  
private func somePrivateFunction() {}
分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址