自主访问控制 强制访问控制

In this tutorial, we’ll discuss about Swift Access Control. Before we get a hang of it, let’s understand an important term that we’ll be using throughout this tutorial: Modules.

在本教程中,我们将讨论Swift Access Control。 在掌握它之前,让我们了解一个将在本教程中使用的重要术语: Modules

Modules are just a bundle of code that is used in other Modules through the import statement. In simpler terms, a Module is just a folder in which you hold your swift files.

模块只是通过import语句在其他模块中使用的一捆代码。 简单来说,模块只是一个文件夹,您可以在其中保存快速文件。

快速访问控制 (Swift Access Control)

Access Control handles the restrictions of your code. By defining the access of your classes, functions, properties, enumerations, protocols, you have the power to use these stuff at places you want to, and hide it from where you don’t need it, in your XCode Project.

访问控制处理您的代码的限制。 通过定义对类 , 函数 , 属性 , 枚举 , 协议的访问 ,您可以在XCode Project中的所需位置使用这些内容,并将其隐藏在不需要的地方。

Note: Whenever we write entities in this tutorial, it’ll refer to these: classes, functions, properties, enumerations, protocols.

Note :每当我们在本教程中编写实体时,它都会引用以下内容:类,函数,属性,枚举,协议。

快速访问级别 (Swift Access Levels)

Following are the major levels that provide you with access control. They’re ordered from least restricted to the highest.

以下是为您提供访问控制的主要级别。 按从最低到最高的顺序排列。

  • open: Least restrictive. Entities can be accessed from any file in the current module or a different module files.
    classes. Classes/Members with open modifier can be inherited/overridden anywhere in the current module or different module.open :限制性最小。 可以从当前模块中的任何文件或其他模块文件中访问实体。
    类。 带有open修饰符的类/成员可以在当前模块或其他模块中的任何位置继承/重写。
  • public: Less restrictive. Entities can be accessed in the same or different module.
    Classes/Members with open modifier cannot be inherited/overridden anywhere in modules other than the current module.public :限制较少。 可以在相同或不同的模块中访问实体。
    具有open修饰符的类/成员不能在当前模块以外的任何其他模块中继承/重写。
  • internal: Default access level. If no access level is specified, this is the default access level. It allows entities to be used within any source file in their defined module but not outside that module. The same rule applies to inheritance too.internal :默认访问级别。 如果未指定访问级别,则这是默认访问级别。 它允许实体在其定义的模块中的任何源文件中使用,但不能在该模块之外使用。 同样的规则也适用于继承。
  • fileprivate: This access level restricts the use of entities to the current file only. Subclassing a fileprivate class would require you to specify the subclass access level to fileprivate or a more restrictive one.fileprivate :此访问级别将实体的使用限制为仅当前文件。 子类化fileprivate类将需要您指定子类的访问级别以fileprivate或更严格。
  • private: Most restrictive. Entities with this access level can be accessed only in the enclosed scope. Also, any extension to that declaration has the access too.private :最严格的。 具有此访问级别的实体只能在随附的范围内访问。 同样,对该声明的任何扩展都可以访问。

The following flow aptly shows the access levels for each.

以下流程恰当地显示了每个访问级别。

快速访问控制示例 (Swift Access Control Example)

The syntax of classes and properties looks like this:

类和属性的语法如下所示:

open class AnOpenClass {}
public class APublicClass {}
internal class ExplicitInternalClass {}
fileprivate class AFilePrivateClass {}
private class APrivateClass {}open func AnOpenFunction() {}
public var APublicVariable = 0
internal let ExplicitInternalConstant = 0
fileprivate func AFilePrivateFunction() {}
private func APrivateFunction() {}func ImplicitInternalFunction(){}
class ImplicitInternalClass {}

快速访问控制–属性 (Swift Access Control – Properties)

A Property – var, let cannot have more access that the type it is assigned to.

一个Property – var,让我们不能对其分配的类型拥有更多的访问权限。

class A {}
public var a = A() //compiler error.open class B{//Your code
}
public var b = B()private class C{//Your code
}
private var c = C()

class A is internal, so it’ll fail to compile when a is public(more access)
A private class has to have a private property defined.

class A是内部的,因此当a是公共的(更多访问权限)时,它将无法编译
私有类必须定义一个私有属性。

Swift访问控制Getter和Setter方法 (Swift Access Control Getter and Setter Methods)

The getters and setters on a property would get the same access level as defined on the property.
We can set a different access level on the setter too.
But, the setter cannot have a higher access level than the property.
A fileprivate getter and setter property:

属性上的获取器和设置器将获得与该属性定义的访问级别相同的访问级别。
我们也可以在设置器上设置不同的访问级别。
但是,setter不能具有比该属性更高的访问级别。
一个fileprivate getter和setter属性:

var length : Double = 5.0
let breadth : Double = 1.1fileprivate var area : Double {get{return length*breadth}set(newArea){length = newArea/breadth}
}

The getter will always have the same access level as the property.
The following is a public getter and a private setter.

该getter将始终具有与该属性相同的访问级别。
以下是公共获取者和私有获取者。

var length : Double = 5.0
let breadth : Double = 1.1public private(set) var area : Double {get{return length*breadth}set(newArea){length = newArea/breadth}
}

The following would fail since the setter has more access than the property and its getter

由于设置器比属性及其获取器具有更多的访问权限,因此以下操作将失败

var length : Double = 5.0
let breadth : Double = 1.1private fileprivate(set) var area : Double { //compiler errorget{return length*breadth}set(newArea){length = newArea/breadth}
}

Swift访问控制–类 (Swift Access Control – Classes)

The access level defined for a class may or may not be applied to the members and functions.
A class with public access means that its members will have internal access by default unless stated otherwise.
The following image shows two classes. The private members of one class cannot be accessed in the other.

为类定义的访问级别可以应用于也可以不应用于成员和功能。
具有公共访问权限的类意味着,除非另有说明,否则默认情况下其成员将具有内部访问权限。
下图显示了两个类。 一类的私人成员无法在另一类中访问。

子类的访问控制 (Access control with Subclasses)

Rule 1: A subclass cannot have a higher access level than it’s superclass.
规则1:子类不能具有比其父类更高的访问级别。

The following example won’t compile.

以下示例将无法编译。

class A {//Internal class
}public class B : A {//public subclass not allowed.
}
Rule 2: Overridden methods can have a higher access level than their superclass counterparts.
规则2:覆盖的方法可以具有比其超类对应方法更高的访问级别。
class A {//Internal classfileprivate func hey() {print("Hey")}internal func hello() {print("Hello")}
}fileprivate class B : A {override public func hey() {print("B")}override fileprivate func hello() {print("B")}
}
Rule 3: A private function cannot be overridden. An overridden function cannot be private.
规则3:私有功能不能被覆盖。 覆盖的函数不能是私有的。
class A {//Internal classfileprivate func hey() {print("Hey")}internal func hello() {print("Hello")}
}fileprivate class B : A {override open func hey() {print("B")}override private func hello() { //compiler error.print("B")}
}

快速访问控制–元组 (Swift Access Control – Tuples)

The access level of a Swift Tuple would be the level of the most restrictive element among the elements present.

Swift元组的访问级别将是存在的元素中限制性最强的元素的级别。

fileprivate class F1{var str : Stringvar number: Intinit(s: String, n: Int) {str = snumber = n}
}
private class F2{var str : Stringvar number: Intinit(s: String, n: Int) {str = snumber = n}
}
class G{//compiler error below.var tuple : (F1,F2) =  (F1(s: "Anupam",n: 101), F2(s: "JournalDev.com",n: 102))
}

In the above code, the access level of the tuple must be set to private, the more restrictive of the two levels.

在上面的代码中,必须将元组的访问级别设置为私有,这两个级别中的限制更多。

private var tuple : (F1,F2) =  (F1(s: "Anupam",n: 101), F2(s: "JournalDev.com",n: 102))

具有功能类型的Swift访问控制 (Swift Access Control with Function Types)

Functions in swift, like Tuples, have the access level equal to the most restrictive level among the parameters and the return type. But you have to specify the access level of the functions explicitly too.
If the specified access level does not match the calculated one, it’ll throw a compiler error.
The below code looks fine but it would NOT compile:

快速的函数(如元组)具有的访问级别等于参数和返回类型中最严格的级别。 但是,您也必须明确指定功能的访问级别。
如果指定的访问级别与计算出的访问级别不匹配,则会引发编译器错误。
以下代码看起来不错,但无法编译:

fileprivate class F1{var str : Stringvar number: Intinit(s: String, n: Int) {str = snumber = n}
}
class G{func hey(s: String,y: Int) -> F1 //compiler error.{print("\(s) and \(y)")return F1(s: s, n: y)}
}

The return type of hey() is F1 which is a fileprivate class. Hence you must make the function fileprivate.

Hey hey()的返回类型是F1 ,这是一个文件私有类。 因此,您必须使函数fileprivate。

具有初始化功能的Swift Access Control (Swift Access Control with init function)

Swift Initialisers must have the same or lesser access level than its parameters defined.
The following code defines an initaliser in class G that would fail since it has higher access level.

Swift初始化程序必须具有与其定义的参数相同或更低的访问级别。
以下代码在类G中定义了一个初始化器,由于其具有更高的访问级别,该初始化器将失败。

fileprivate class F1{var str : Stringvar number: Intinit(s: String, n: Int) {str = snumber = n}
}
private class F2{var str : Stringvar number: Intinit(s: String, n: Int) {str = snumber = n}
}
class G{private var tuple : (F1,F2)init(f1: F1, f2: F2) { //compiler error.tuple = (F1(s: "Anupam",n: 101), F2(s: "JournalDev.com",n: 102))}
}

We can set the initialiser above to either of the fileprivate or private.

我们可以将上面的初始化程序设置为fileprivateprivate

Required initialisers follow the same access rule as initialisers along with the rule that the access level should be the same as that of the enclosed class.
所需的初始化程序遵循与初始化程序相同的访问规则,并遵循以下规则:访问级别应与封闭类的访问级别相同。

So just keeping the fileprivate level on the required init would give the following error.

因此,仅将fileprivate级别保持在所需的init上将产生以下错误。

So either make the parameters as the same type of the class OR vice-versa. Here we’ll do the later and make the class as fileprivate.
Continuing with our previous code, using required init would make the class G look like this:

因此,要么将参数设为类的相同类型,要么反之亦然。 在这里,我们稍后再做,使该类成为fileprivate
继续我们之前的代码,使用必需的init将使class G看起来像这样:

fileprivate class G{private var tuple : (F1,F2)required fileprivate init(f1: F1, f2: F2) {tuple = (F1(s: "Anupam",n: 101), F2(s: "JournalDev.com",n: 102))}
}

结构类型的默认成员级初始化程序 (Default Memberwise Initializers for Structure Types)

For structure initialiser we must set the initialiser to the following level based on the members

对于结构初始化程序,我们必须根据成员将初始化程序设置为以下级别

  • If any of the initialiser members is private or fileprivate the init should be fileprivate如果任何初始化程序成员是privatefileprivate private ,则初始化应为文件fileprivate
  • Else the init by default is internal.否则,默认情况下,init是内部的。

Continuing with our previous code of fileprivate F1 and private F2 classes, the following code snippet is for the structure’s default initialiser.

继续我们先前的文件fileprivate F1和private F2类的代码,以下代码段适用于该结构的默认初始化程序。

struct StructureExample {
private var tuple : (F1,F2)fileprivate init(f1: F1, f2: F2) {tuple = (F1(s: "Anupam",n: 101), F2(s: "JournalDev.com",n: 102))}
}

Recap: The tuple must be set to an access level lower or equal to its elements levels

总结:元组必须设置为低于或等于其元素级别的访问级别

快速访问控制示例–协议 (Swift Access Control Example – Protocols)

Protocol access level must be set at the time of defining that protocol.
The protocol functions/requirements must have the same access level defined.
Just like Subclassing, if a protocol inherits from another protocol, it cannot have a higher access level that the inherited one.

协议访问级别必须在定义该协议时设置。
协议功能/要求必须具有相同的访问级别。
就像子类化一样,如果一个协议继承自另一个协议,则它不能具有比继承的协议更高的访问级别。

private protocol ProtocolA{//Your code
}private protocol ProtocolB : ProtocolA{//Your code
}

You cannot set ProtocolB to any higher level such as fileprivate.

您不能将ProtocolB设置为任何更高级别,例如fileprivate

Protocol Conformance

协议一致性

  • A class can conform to a protocol with a lower access level.类可以符合访问级别较低的协议。
  • If a class is public and a protocol is private then the type conformance is private.如果类是公共的,而协议是private则类型一致性是private
  • It always the lower one that’s considered.它总是被认为是较低的。
  • If the protocol methods are implemented in the class/structure/enum, they must be set to an access level that is equal to the protocol access level or higher than that.如果协议方法是在类/结构/枚举中实现的,则必须将它们设置为等于协议访问级别或更高的访问级别。

快速访问控制–枚举 (Swift Access Control – Enumerations)

The access level for an enumeration would be applied across all its cases

枚举的访问级别将应用于所有情况

private enum Color{case redcase blue
}

red and blue have the same access type private

红色和蓝色具有相同的访问类型private

The access level of the raw values should be greater than or equal to the enum access level.
原始值的访问级别应大于或等于枚举访问级别。

快速访问控制–扩展 (Swift Access Control – Extensions)

Any type members added in an extension must have the same default access level as type members declared in the original type being extended.
To change the access level to a different one from the class, you just need to add the access modifier on the extension and it’ll change the default access levels of the extensions members too unless you set the levels on members explicitly.

在扩展中添加的任何类型成员必须具有与在要扩展的原始类型中声明的类型成员相同的默认访问级别。
要将访问级别更改为与该类不同的访问级别,只需要在扩展上添加访问修饰符,除非您在成员上明确设置了访问级别,否则它也会更改扩展成员的默认访问级别。

You cannot declare a private/fileprivate class with an extension of public or internal or open

您不能声明具有public或internal或open扩展名的private / fileprivate类

private class H{private var hello = "Hi"
}
//compiler error below
public extension H{var newHello:String  {return "Yeah"}
}

We can access private members from a class/struct in their extensions.
We can access private members from one extension in another.

我们可以从扩展名中的类/结构访问私有成员。
我们可以从另一扩展名访问私人成员。

快速访问控制–泛型和类型别名 (Swift Access Control – Generics and Type Aliases)

  • The access level for a generic type or generic function is the minimum of the access level of the generic type or function itself and the access level of any of its members/return type.通用类型或通用功能的访问级别是通用类型或功能本身的访问级别及其任何成员/返回类型的访问级别的最小值。
  • A type alias can have an access level less than or equal to the access level of the type it aliases.
    This is the same as subclassing. Type alias cannot use a higher access level than it’s type.类型别名的访问级别可以小于或等于其别名的访问级别。
    这与子类化相同。 类型别名不能使用比其类型更高的访问级别。

翻译自: https://www.journaldev.com/19795/swift-access-control

自主访问控制 强制访问控制

自主访问控制 强制访问控制_快速访问控制相关推荐

  1. 【web-攻击访问控制】(5.3)保障访问控制的安全:多层权限模型

    目录 一.保障访问控制的安全 简介: 多层权限模型 简述: 过程: 一.保障访问控制的安全 简介: 访问控制是最容易理解的Web应用程序安全领域, 但是在执行它们时必须采用合理.全面的方法. 首先应避 ...

  2. Nginx 连接限制和访问控制Nginx 连接限制和访问控制

    Nginx 连接限制和访问控制   前言 Nginx自带的模块支持对并发请求数进行限制, 还有对请求来源进行限制.可以用来防止DDOS攻击. 阅读本文须知道nginx的配置文件结构和语法. 连接限制 ...

  3. 控制访问列表_为什么访问控制列表这么难

    控制访问列表 We've all been there. There's a critical delivery, application teams are in a panic requestin ...

  4. Java的访问控制修饰符_访问权限修饰符_访问权限修饰词

    public > protected > (default) > private 1.public 修饰的,在任何地方都可以访问 2.protected 修饰的,在类内部.同一个包. ...

  5. 标准访问控制列表和扩展的访问控制列表有什么区别??

    原文链接:https://zhidao.baidu.com/question/344680488.html 标准访问控制列表 : 根据数据包的源IP地址来允许或拒绝数据包. 访问控制列表号是1-99以 ...

  6. 计算机网络访问控制列表,南昌大学计算机网络实验-访问控制列表ACL

    南昌大学实验报告 学生姓名: 学 号: 专业班级: 实验类型:■ 验证 □ 综合 □ 设计 □ 创新 实验日期: 实验成绩: 实验4:访问控制列表ACL配置实验 一.实验目的 对路由器的访问控制列表A ...

  7. 成员的访问控制java_java类成员的访问控制(翻译自Java Tutorials)

    原文出自 http://www.cnblogs.com/ggjucheng/archive/2012/11/28/2793339.html 访问级别修饰符决定了其他类是否可以使用它的特定字段或调用特定 ...

  8. 计算机网络之数据链路层:8、介质访问控制之信道划分介质访问控制

    数据链路层:8.介质访问控制-信道划分介质访问控制 思维导图: 俩种链路: 介质访问控制: 多路复用技术: 信道划分介质访问控制(静态划分信道): 频分多路复用FDM: 时分复用技术TDM: 波分复用 ...

  9. 将访问控制更改为:共享级访问控制,并重启计算机使设置生效.,Internet应用模拟题...

    Internet应用模拟题一 1. 查看网上邻居中计算机ZXRD_5下D盘所共享的资源. 2. 打开网上邻居,将计算机ZXRD_5下的D盘映射为J盘网络驱动器. 3. 添加局域网中计算机ZXRD_6下 ...

最新文章

  1. 公众平台服务号、订阅号、企业号的相关说明
  2. UNIX再学习 -- 发送信号
  3. 基于Elasticsearch的数据报表方案
  4. mysql 复制诊断_诊断Mysql复制问题
  5. 如何删除空文件夹Java_JAVA实现将磁盘中所有空文件夹进行删除的代码
  6. PAT-1127. ZigZagging on a Tree (30)
  7. Github(4)-远程操作
  8. 分割文本_PSENet、PANNet、DBNet三个文本检测算法异同
  9. 一文详解opencv摄像头数字识别
  10. ex is not shell_我使用过的Linux命令之exit - 退出当前shell
  11. varchar(10)与nvarchar(10)有什么区别
  12. 多格式无水印录屏软件
  13. iphone6主板注释
  14. 微信api接口调用-给微信好友或群聊发消息
  15. Win10 资源管理器导航栏设置
  16. python语言培训是密封式的吗
  17. 打字时禁用触摸板_触摸打字感觉不错,但不适合我
  18. python爬虫学习-scrapy爬取链家房源信息并存储(翻页)
  19. java sign函数_C / C ++中是否有标准的符号函数(signum,sgn)?
  20. 蓝桥杯 填空题 水题 等差素数列 C++ 简单暴力枚举

热门文章

  1. PHP魔术方法小结.md
  2. 捕获事件要比冒泡事件先触发
  3. FreeMarker 基础语法教程
  4. [转载] windows下python包的导入方法
  5. [转载] Python 学习 - Numpy
  6. [转载] 用python写的一个简单的记事本
  7. [转载] Python(析构函数)
  8. [转载] Java8新特性-003-Java8接口中的default修饰符
  9. hdu1113 Word Amalgamation(详解--map和string的运用)
  10. Paxos—以选美比赛为例PPT