如何才能将类从一个文件访问另一个文件中的另一个类(有点像朋友类功能)

时间:2021-11-09 15:57:25

Say I have 2 class types: DataObject, and DataObjectManager, and they each have their own swift files to keep things neat and clean.

假设我有两种类型:DataObject和DataObjectManager,它们各自都有自己的swift文件,以保持整洁干净。

The DataObjectManager does what the name implies: it manages DataObjects. The DataObjectManager is a singleton, and it owns instances of DataObject, particularly an activeDataObject, which is the getter that the rest of the project uses to get the DataObject that holds the data they need.

DataObjectManager执行名称所暗示的内容:它管理DataObjects。 DataObjectManager是一个单例,它拥有DataObject的实例,特别是一个activeDataObject,它是项目其余部分用来获取保存所需数据的DataObject的getter。

I don't want the other modules of code in my project to be able to change the properties of the activeDataObject, so I've defined almost all of it's properties with the private(set) access control. This keeps the rest of the code from being able to modify the properties of a DataObject. But this also keep the DataObjectManager from being able to modify them. Additionally, I only want the DataObjectManager to be able to create DataObject instances, but I can't declare the init as private because the DataObjectManager is in a different file.

我不希望我的项目中的其他代码模块能够更改activeDataObject的属性,因此我使用私有(设置)访问控制定义了几乎所有的属性。这使得其余代码无法修改DataObject的属性。但这也使DataObjectManager无法修改它们。另外,我只希望DataObjectManager能够创建DataObject实例,但我不能将init声明为private,因为DataObjectManager位于不同的文件中。

I know of one solution, which would be to move all of the DataObject class code into the top (or bottom) of the DataObjectManager's class file, but I have them in separate files not for functionality, but for tidiness.

我知道一个解决方案,它将所有DataObject类代码移动到DataObjectManager的类文件的顶部(或底部),但我将它们放在单独的文件中,不是为了功能,而是为了整洁。

Is there any way to keep my separate-file-tidiness and still get the access control I'm looking for? Or is my only option to just throw the DataObject class into the DataObjectManager's class file (or class directly), and mark all of the properties as private(set) and the init as private?

有没有办法保持我的单独文件整洁,仍然得到我正在寻找的访问控制?或者,我唯一的选择是将DataObject类放入DataObjectManager的类文件(或类直接),并将所有属性标记为private(set),将init标记为private?

Edit: See this radar to understand sort of what I mean. http://www.openradar.appspot.com/17136480

编辑:看看这个雷达,了解我的意思。 http://www.openradar.appspot.com/17136480

1 个解决方案

#1


You currently only have three options for access modifiers in Swift.

您目前在Swift中只有三个访问修饰符选项。

  • internal - this is the default state. It means you can access the class/function/property in any other file in the same target.
  • internal - 这是默认状态。这意味着您可以访问同一目标中任何其他文件中的类/函数/属性。

  • private - as you know, this restricts access to only the current file.
  • private - 如您所知,这限制了对当前文件的访问。

  • public - this allows access from any file in any target.
  • public - 允许从任何目标中的任何文件进行访问。

If you can't do what you need with those three rules, then you can't do it.

如果你不能用这三条规则做你需要的事情,那你就做不到。

So you have to decide which is "messier" - having both classes in the same source file (which, assuming only your DataObjectManager is actually used outside the file, seems acceptable), or keeping the init method and properties internal.

所以你必须决定哪个是“杂乱无章” - 在同一个源文件中有两个类(假设只有你的DataObjectManager实际上在文件外部使用,似乎可以接受),或者保持init方法和属性是内部的。

Source: The Swift Programming Language

来源:Swift编程语言

#1


You currently only have three options for access modifiers in Swift.

您目前在Swift中只有三个访问修饰符选项。

  • internal - this is the default state. It means you can access the class/function/property in any other file in the same target.
  • internal - 这是默认状态。这意味着您可以访问同一目标中任何其他文件中的类/函数/属性。

  • private - as you know, this restricts access to only the current file.
  • private - 如您所知,这限制了对当前文件的访问。

  • public - this allows access from any file in any target.
  • public - 允许从任何目标中的任何文件进行访问。

If you can't do what you need with those three rules, then you can't do it.

如果你不能用这三条规则做你需要的事情,那你就做不到。

So you have to decide which is "messier" - having both classes in the same source file (which, assuming only your DataObjectManager is actually used outside the file, seems acceptable), or keeping the init method and properties internal.

所以你必须决定哪个是“杂乱无章” - 在同一个源文件中有两个类(假设只有你的DataObjectManager实际上在文件外部使用,似乎可以接受),或者保持init方法和属性是内部的。

Source: The Swift Programming Language

来源:Swift编程语言