模块在 swift 中意味着什么? [英] What does a module mean in swift?

查看:34
本文介绍了模块在 swift 中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有两个名为 file1.swiftfile2.swift 的文件.

For example, I have two files called file1.swift and file2.swift.

file1.swift:

import UIKit

class A: B {
}

file2.swift:

import UIKit

class C: A{     
}

我读到公共类不能在模块之外进行子类化.这里我有子类 C.我试图理解模块在这里的含义.我为这两个文件导入了相同的模块 UIKit.所以这两个文件是同一个模块?这样我就可以子类化了.或者即使我导入相同的 UIKit,两个文件也有不同的模块?

I am reading that public class can not subclassed outside of module. Here I have subclass C. I am trying to understand what does module mean here. I imported to same module UIKit for both file. So the both files are of same module? So that I can subclassed. Or both files have different module even I import the same UIKit?

谁能解释一下什么是模块?

Can anybody explain what is module?

来源:

具有公共访问权限或更严格的访问级别的类只能在定义它们的模块内进行子类化.

Classes with public access, or any more restrictive access level, can be subclassed only within the module where they’re defined.

具有公共访问权限或更严格的访问级别的类成员只能在定义它们的模块内被子类覆盖.

Class members with public access, or any more restrictive access level, can be overridden by subclasses only within the module where they’re defined.

推荐答案

模块是代码分发的单个单元——作为单个单元构建和交付的框架或应用程序,可以由另一个模块使用 Swift 的 import 关键字导入.

A module is a single unit of code distribution—a framework or application that is built and shipped as a single unit and that can be imported by another module with Swift’s import keyword.

Xcode 中的每个构建目标(例如应用程序包或框架)在 Swift 中都被视为一个单独的模块.如果您将应用程序代码的各个方面组合在一起作为一个独立的框架——可能是在多个应用程序中封装和重用该代码——那么当你在应用程序中导入和使用时,你在该框架内定义的所有内容都将成为单独模块的一部分,或者当它在另一个框架中使用时.

Each build target (such as an app bundle or framework) in Xcode is treated as a separate module in Swift. If you group together aspects of your app’s code as a stand-alone framework—perhaps to encapsulate and reuse that code across multiple applications—then everything you define within that framework will be part of a separate module when it’s imported and used within an app, or when it’s used within another framework.

正如 docs 所示,模块是一个应用程序或一个框架(库).如果您创建一个包含 AB 类的项目,它们是同一个模块的一部分.同一项目中的任何其他类都可以从这些类继承.但是,如果您将该项目导入到另一个项目,则来自该另一个项目的类将无法子类化 AB.为此,您必须在声明之前添加 open 指示符.

As the docs indicate, the module is an application or a framework (library). If you create a project with classes A and B, they are part of the same module. Any other class in the same project can inherit from those classes. If you however import that project to another project, classes from that another project won't be able to subclass A nor B. For that you would have to add open indicator before their declarations.

基本上,如果您在一个应用程序上工作,那么您就是在一个模块中工作,除非声明为 privatefileprivate,否则这些类可以相互继承.

Basically, if you work on a single app then you are working in one single module and unless declared as private or fileprivate, the classes can subclass each other.

编辑

让我们在模块(项目)Module1中有以下类:

Let us have following class in module (project) Module1:

class A {
}

由于这个类不是open,它只能在同一个模块内被子类化.这意味着以下课程:

Since this class is not open, it can be subclassed only within the same module. That means that following class:

class B: A {
}

可以写在同一个项目中,Module1.

Can be written only in the same project, in Module1.

如果您将 Module1 作为依赖项添加到项目 Module2,并尝试这样做:

If you add Module1 as a dependency to project Module2, and try to do this:

import Module1

class C: A {
}

它不会编译.这是因为 A 类不是 open(换句话说,它可以访问 public 或更少)并且它不属于与 public 相同的模块代码>C.A属于Module1C属于Module2.

It will not compile. That's because class A is not open (in other words it has access public or less) and it does not belong to the same module as C. A belongs to Module1, C belongs to Module2.

注意

import 关键字将依赖模块导入到当前模块中.如果你在你的项目中编写了 import UIKit,你就是在告诉编译器你想在你的模块中使用 UIKit 模块.import 没有定义当前模块.当前模块是当前项目.

import keyword imports a dependency module to your current module. If you write import UIKit in your project, you are telling the compiler that you want to use module UIKit in your module. import does not define current module. Current module is the current project.

在文件开头添加 import UIKit 不会改变也不会定义文件属于哪个模块.它只是告诉编译器你想在那个文件中使用来自 UIKit 模块的代码.

Adding import UIKit at the beginning of the file does not change nor define to which module the file belongs. It just tells the compiler that in that file you want to use code from UIKit module.

这篇关于模块在 swift 中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆