在Swift中,Access修饰符内部和公共之间有什么区别?

时间:2022-09-02 09:48:59

Swift offers 5 access modifiers: open, public, internal, fileprivate and private.

Swift提供5种访问修饰符:open,public,internal,fileprivate和private。

Of what I know about these specifiers, (mainly from link & link_2)

我对这些说明符的了解,(主要来自link和link_2)

open means classes and class members can be subclassed and overridden both within and outside the defining module (target).

open意味着类和类成员可以在定义模块(目标)的内部和外部进行子类化和覆盖。

fileprivate restricts the use of an entity to its defining source file. Basically accessible by multiple classes within a single file.

fileprivate将实体的使用限制在其定义的源文件中。基本上可以通过单个文件中的多个类访问。

private restricts the use of an entity to its enclosing declaration.

private将实体的使用限制在其附件声明中。

Now, public and internal seems pretty much the same to me :-

现在,公众和内部对我来说几乎是一样的: -

public means classes and class members can only be subclassed and overridden within the defining module (target).

public意味着类和类成员只能在定义模块(目标)中进行子类化和重写。

internal enables an entity to be used within the defining module (target). Also, this happens to be the default specifier if nothing else is mentioned. We would typically use internal access when defining an app’s or a framework’s internal structure.

internal允许实体在定义模块(目标)中使用。此外,如果没有提到其他内容,这恰好是默认说明符。在定义应用程序或框架的内部结构时,我们通常会使用内部访问。

So basically how do public and internal differ?

那么公共和内部基本上有何区别?

This is my first Question here, so if I have missed out any details, please let me know. Thanks in advance.

这是我的第一个问题,如果我错过了任何细节,请告诉我。提前致谢。

2 个解决方案

#1


2  

Whatever you marked as public can be use within your app and outside of you app(module). If you marked something as internal that can only be used within your app(module). This is very helpful when your developing a library (framework) , you can use internal to hide library structure.
And Public members of A.swift and B.swift are available to C.swift and D.swift. The only restriction is that classes can't be subclassed (they would need to be open.) - My answer base on @Keaz & @Alexander.

无论您标记为公共,都可以在您的应用程序内以及您的应用程序(模块)之外使用。如果您将某些内容标记为内部,只能在您的应用(模块)中使用。这对于开发库(框架)非常有用,可以使用内部隐藏库结构。 A.swift和D.swift的公共成员可以使用C.swift和D.swift。唯一的限制是类不能被子类化(它们需要打开。) - 我的答案基于@Keaz和@Alexander。

#2


1  

From Access Control manual:

从访问控制手册:

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. You typically use open or public access when specifying the public interface to a framework. The difference between open and public access is described below.

开放访问和公共访问使实体可以在其定义模块的任何源文件中使用,也可以在来自导入定义模块的另一个模块的源文件中使用。在指定框架的公共接口时,通常使用开放或公共访问。开放和公共访问之间的区别如下所述。

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.

内部访问使实体可以在其定义模块的任何源文件中使用,但不能在该模块之外的任何源文件中使用。在定义应用程序或框架的内部结构时,通常使用内部访问。

Difference is in visibility to other modules.

差异在于对其他模块的可见性。

EDIT to answer @iCode comment:

编辑回答@iCode评论:

You don't need all of them.

你不需要所有这些。

For simplest small single-dev application just using default internal will be enough.

对于最简单的小型单开发应用程序,仅使用默认内部就足够了。

If you will need to do it right you may add fileprivate/private accessors to hide some implementation.

如果您需要正确操作,可以添加fileprivate / private访问器来隐藏一些实现。

If you're developing large app and want to separate code into modules, or if you're developing library you will need to use public/open to create inter-module interface.

如果您正在开发大型应用程序并希望将代码分离到模块中,或者如果您正在开发库,则需要使用public / open来创建模块间接口。

#1


2  

Whatever you marked as public can be use within your app and outside of you app(module). If you marked something as internal that can only be used within your app(module). This is very helpful when your developing a library (framework) , you can use internal to hide library structure.
And Public members of A.swift and B.swift are available to C.swift and D.swift. The only restriction is that classes can't be subclassed (they would need to be open.) - My answer base on @Keaz & @Alexander.

无论您标记为公共,都可以在您的应用程序内以及您的应用程序(模块)之外使用。如果您将某些内容标记为内部,只能在您的应用(模块)中使用。这对于开发库(框架)非常有用,可以使用内部隐藏库结构。 A.swift和D.swift的公共成员可以使用C.swift和D.swift。唯一的限制是类不能被子类化(它们需要打开。) - 我的答案基于@Keaz和@Alexander。

#2


1  

From Access Control manual:

从访问控制手册:

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. You typically use open or public access when specifying the public interface to a framework. The difference between open and public access is described below.

开放访问和公共访问使实体可以在其定义模块的任何源文件中使用,也可以在来自导入定义模块的另一个模块的源文件中使用。在指定框架的公共接口时,通常使用开放或公共访问。开放和公共访问之间的区别如下所述。

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.

内部访问使实体可以在其定义模块的任何源文件中使用,但不能在该模块之外的任何源文件中使用。在定义应用程序或框架的内部结构时,通常使用内部访问。

Difference is in visibility to other modules.

差异在于对其他模块的可见性。

EDIT to answer @iCode comment:

编辑回答@iCode评论:

You don't need all of them.

你不需要所有这些。

For simplest small single-dev application just using default internal will be enough.

对于最简单的小型单开发应用程序,仅使用默认内部就足够了。

If you will need to do it right you may add fileprivate/private accessors to hide some implementation.

如果您需要正确操作,可以添加fileprivate / private访问器来隐藏一些实现。

If you're developing large app and want to separate code into modules, or if you're developing library you will need to use public/open to create inter-module interface.

如果您正在开发大型应用程序并希望将代码分离到模块中,或者如果您正在开发库,则需要使用public / open来创建模块间接口。