问题描述
我已经通读了 Apple 的 Swift 文档,但找不到任何关于如何创建模块或如何将类或结构成员定义为私有或公共的信息.
I have read through Apple's documentation for Swift and can find nothing about how to create modules or how to define class or stucture members as private or public.
语法中有对 import 语句的引用,但我找不到有关它的作用或如何使用它的信息.
There are references to the import statement in the syntax but I can find no information on what it does or how to use it.
有人知道我在哪里可以找到这个吗?
Does anyone know where I can find this?
推荐答案
更新
您可以使用框架对 swift 项目进行模块化.
You can modularize a swift project using frameworks.
我们通过为每个模块创建单独的框架项目并通过 Xcode 工作区链接它们来进行模块化.当我们将组件分成不同的项目时看起来更自然它还确保模块之间只有单向通信.开发人员可以在不考虑其他模块的情况下进行隔离工作/测试.
We modularize by creating separate framework projects for each module and link them via Xcode workspace. It looks more natural when we separate the components into different projects & it also makes sure that there is only a one-way communication between modules. Developers can work/test on isolation without thinking much about other modules.
默认情况下,在框架内创建的类/结构/等将具有 internal
访问控制类型,因此它仅在模块内可见.为了让它在模块外可见,你可以让它public
.
By default classes/structs/etc created within a framework will have an internal
access control type so it is only visible within the modules. In order to make it visible outside the module, you can make it public
.
有关访问控制级别的更多信息此处
More info on access controll level here
最新的 Xcode 6 beta 更新(beta 4)为 swift 带来访问控制
The latest Xcode 6 beta update (beta 4) bring access control to swift
Swift 启用访问控制
Swift 访问控制具有三个访问级别:
Swift access control has three access levels:
- 私有 实体只能从定义它们的源文件中访问.
- 内部实体可以在定义它们的目标内的任何地方访问.
- public 实体可以从目标内的任何地方以及从导入当前目标模块的任何其他上下文访问.
- private entities can only be accessed from within the source file where they are defined.
- internal entities can be accessed anywhere within the target where they are defined.!
- public entities can be accessed from anywhere within the target and from any other context that imports the current target’s module.
这篇关于如何在 Swift 中创建可导入模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!