swift5 访问控制_Swift中的访问控制

swift5 访问控制

A brief discussion of different access control in the Swift programming language.

简要讨论Swift编程语言中的不同访问控制。

总览 (Overview)

According to the swift official documentation

根据Swift的官方文件

Access control restricts access to parts of your code from code in other source files and modules. This feature enables you to hide the implementation details of your code, and to specify a preferred interface through which that code can be accessed and used.

访问控制限制从其他源文件和模块中的代码访问部分代码。 使用此功能,您可以隐藏代码的实现细节,并指定可以访问和使用该代码的首选接口。

Swift 5 has five access control. They are open, public, internal, fileprivate, and private. Those access controls are defined based on the uses in the module and source file. In this tutorial, we will learn what, where, and how to use access control in swift.

Swift 5具有五个访问控制。 它们是开放的,公共的,内部的,文件私有的和私有的。 这些访问控制是根据模块和源文件中的用途定义的。 在本教程中,我们将学习如何快速,什么地方以及如何使用访问控制。

1.开放(限制较少) (1. open (less restrictive))

Anything defines with open can be accessed within or without the module. open access level entities can be accessed from anywhere. That means an open variable can access, an open function can override and open class can be subclass inside of a module and outside of a module by importing that module.

任何定义为open的内容都可以在模块内或模块外访问。 可以从任何地方访问开放访问级别的实体。 这意味着,通过导入该模块,可以访问一个开放变量,一个开放函数可以覆盖并且开放类可以是模块内部和模块外部的子类。

An example is the “UITableViewCell” class in the UIKit framework. We can subclass any class as a UITableViewCell class by importing the UIKit module. If you see the definition of “UITableViewCell”, you will see open access control before the class definition inside the UIKit module. That’s why we can subclassing any class as a UITableViewCell class by importing UIKit.

一个示例是UIKit框架中的“ UITableViewCell ”类。 我们可以通过导入UIKit模块将任何类作为UITableViewCell类进行子类化。 如果看到“ UITableViewCell”的定义,则将在UIKit模块内部的类定义之前看到打开访问控制。 这就是为什么我们可以通过导入UIKit将任何类都继承为UITableViewCell类。

@available(iOS 2.0, *)open class UITableViewCell : UIView, NSCoding, UIGestureRecognizerDelegate { }

2.公开(限制性大于开放性) (2. public (restrictive than open))

Like open access, public entities also can be accessed within or outside of a module. But there is a major difference between open and public.

像开放访问一样,公共实体也可以在模块内或模块外访问。 但是公开和公开之间有很大的区别。

public class can’t be subclassed and public method can’t be overridden outside of the defined module. Must have to subclass or override within the module where they defined.

公共类不能被子类化,并且公共方法不能在定义的模块之外被覆盖。 必须在其定义的模块内继承或覆盖。

Check the example below

检查下面的例子

//module 1open class X{}
open func bar(){}public class Y{}
public func foo(){}//module 2class A: X{} // success
override func bar(){} // successclass B: Y{} //error
override func foo(){} // error

3.内部(模块级访问控制) (3. internal ( module-level access control))

“internal” enable to use of entities anywhere inside the defined module. “internal” is the default access level. Normally a module is a framework or app which can be used as a single unit.

“内部”使得可以在已定义模块内部的任何位置使用实体。 “内部”是默认访问级别。 通常一个模块是 可以用作单个单元的框架或应用。

Inside a target (module) all the entities are by default internal (If explicitly did not define any access control). It’s defined that way that everything inside the module can be accessed by anyone from anywhere within that module.

在目标(模块)内部,所有实体默认情况下都是内部实体(如果未明确定义任何访问控制)。 通过这种方式定义,模块内任何地方的任何人都可以访问模块内的所有内容。

4. fileprivate(文件级访问控制) (4. fileprivate( file-level access control))

fileprivate entities only can be accessible within a swift file. A swift file can have several classes or structures. All the file private entities inside a swift file can be accessed by all of the classes or structures in that file. Without them, any other classes outside of that file can’t access them. It generally hides the implementation from other source files of the module.

只能在swift文件中访问fileprivate实体。 一个快速文件可以具有多个类或结构。 快速文件内的所有文件私有实体都可以被该文件中的所有类或结构访问。 没有它们,该文件之外的任何其他类都无法访问它们。 它通常从模块的其他源文件中隐藏实现。

//**  ViewController.swift **//class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad() let a = A()
a.x()
a.z() //error let b = B()
b.Y() // error
}
}class A {fileprivate func x(){
print("Inside ViewController file")
}
}
//** SecondViewController.swift **//class B{fileprivate func Y(){
print("Outside ViewController file")
}
}extension A{
fileprivate func Z(){
print("Outside ViewController file, extension of A")
}
}

5.私有(类级别的访问控制) (5. private( class-level access control))

private is the most used and most restricted access control. The private entities can access only within the class (including extension) they declared. It is generally used to hide the internal implementation or functionality outside of a specific class.

私有是最常用和最受限制的访问控制。 私有实体只能在其声明的类(包括扩展名)内访问。 它通常用于隐藏特定类之外的内部实现或功能。

// **  ViewController.swift **//class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
x()
let a = A()
a.Y() // error
}
}extension ViewController{
private func x(){
print("Inside x")
}
}class A {
private func Y(){
print("Inside Y")
}
}

快速访问控制之间的区别 (Difference between swift access control)

Now we will see the difference between those access modifier based on different parameters as a tabular form

现在,我们将以表格形式看到基于不同参数的访问修饰符之间的区别

Image for post

Congratulation 🎉 🎉 🎉 I guess now you got some idea about access control in the swift programming language. You will find a brief discussion of them on swift official documentation.

祝贺您,我想您现在对使用快速编程语言的访问控制有了一些了解。 您可以在快速的官方文档中找到有关它们的简短讨论。

If you found this article useful please share and give some clap 👏👏👏Check my other articles on Medium and connect me on LinkedIn.

如果您觉得这篇文章很有用,请分享并给予一些鼓掌 。👏👏👏查看我在Medium上的其他文章,并在LinkedIn上与我联系。

Thank you for reading & Happy coding 🙂

感谢您的阅读和快乐的编码🙂

翻译自: https://levelup.gitconnected.com/access-control-in-swift-98ad5901a358

swift5 访问控制

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值