Xcode 项目与 Xcode 工作区 - 差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21631313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:03:49  来源:igfitidea点击:

Xcode Project vs. Xcode Workspace - Differences

xcode

提问by ms2

I am trying to understand how the whole ecosystem of iOSworks.
Until now, I could find an answer for most of my question (and trust me, there have been a lots of them), but for this one, there seems to be no clear answer yet.

我试图了解整个生态系统是如何iOS运作的。
到目前为止,我可以为我的大部分问题找到答案(相信我,已经有很多答案了),但对于这个问题,似乎还没有明确的答案。

What is the difference between XcodeProject and XcodeWorkspace files?

XcodeProject 和 XcodeWorkspace 文件有什么区别?

  1. What is the difference between the two of them?
  2. What are they responsible for?
  3. Which one of them should I work with when I'm developing my Apps in team/alone?
  4. Is there anything else I should be aware of in matter of these two files?
  1. 他们两个有什么区别?
  2. 他们负责什么?
  3. 当我在团队中/单独开发我的应用程序时,我应该与他们中的哪一个一起工作?
  4. 关于这两个文件还有什么我应该注意的吗?

回答by hagi

I think there are three key items you need to understand regarding project structure: Targets, projects, and workspaces. Targetsspecify in detail how a product/binary (i.e., an application or library) is built. They include build settings, such as compiler and linker flags, and they define which files (source code and resources) actually belong to a product. When you build/run, you always select one specific target.

我认为您需要了解关于项目结构的三个关键项目:Targetsprojectsworkspaces目标详细指定了产品/二进制文件(即应用程序或库)的构建方式。它们包括构建设置,例如编译器和链接器标志,并定义哪些文件(源代码和资源)实际上属于产品。当你构建/运行时,你总是选择一个特定的目标。

It is likely that you have a few targets that share code and resources. These different targets can be slightly different versions of an app (iPad/iPhone, different brandings,…) or test cases that naturally need to access the same source files as the app. All these related targets can be grouped in a project. While the project contains the files from all its targets, each target picks its own subset of relevant files. The same goes for build settings: You can define default project-wide settings in the project, but if one of your targets needs different settings, you can always override them there:

您可能有几个共享代码和资源的目标。这些不同的目标可能是应用程序的稍微不同的版本(iPad/iPhone、不同的品牌,...)或自然需要访问与应用程序相同的源文件的测试用例。所有这些相关的目标都可以在一个项目中分组。虽然项目包含来自其所有目标的文件,但每个目标都会选择自己的相关文件子集。构建设置也是如此:您可以在项目中定义默认的项目范围设置,但如果您的目标之一需要不同的设置,您可以始终在那里覆盖它们:

Shared project settings that all targets inherit, unless they overwrite it

所有目标都继承的共享项目设置,除非它们覆盖它

Shared project settings that all targets inherit, unless they override it

所有目标都继承的共享项目设置,除非它们覆盖它

Concrete target settings: PSE iPhone overwrites the project's Base SDK setting

具体目标设置:PSE iPhone覆盖项目的Base SDK设置

Concrete target settings: PSE iPhoneoverrides the project's Base SDKsetting

具体目标设置:PSE iPhone覆盖项目的Base SDK设置

In Xcode, you always open projects (or workspaces, but not targets), and all the targets it contains can be built/run, but there's no way/definition of building a project, so every project needs at least one target in order to be more than just a collection of files and settings.

在 Xcode 中,您总是打开项目(或工作区,但不是目标),并且它包含的所有目标都可以构建/运行,但是没有构建项目的方法/定义,因此每个项目至少需要一个目标才能不仅仅是文件和设置的集合。

Select one of the project's targets to run

选择要运行的项目目标之一

Select one of the project's targets to run

选择要运行的项目目标之一

In a lot of cases, projects are all you need. If you have a dependency that you build from source, you can embed it as a subproject. Subprojects can be opened separately or within their super project.

在很多情况下,项目就是你所需要的。如果您有从源代码构建的依赖项,则可以将其嵌入为子项目。子项目可以单独打开,也可以在其超级项目中打开。

demoLib is a subprojec

demoLib 是一个子项目

demoLibis a subproject

demoLib是一个子项目

If you add one of the subproject's targets to the super project's dependencies, the subproject will be automatically built unless it has remained unchanged. The advantage here is that you can edit files from both your project and your dependencies in the same Xcode window, and when you build/run, you can select from the project's and its subprojects' targets:

如果将子项目的目标之一添加到超级项目的依赖项中,则子项目将自动构建,除非它保持不变。这里的优点是您可以在同一个 Xcode 窗口中编辑项目和依赖项中的文件,并且在构建/运行时,您可以从项目及其子项目的目标中进行选择:

Running targets from a subproject

从子项目运行目标

If, however, your library (the subproject) is used by a variety of other projects (or their targets, to be precise), it makes sense to put it on the same hierarchy level – that's what workspacesare for. Workspaces contain and manage projects, and all the projects it includes directly (i.e., not their subprojects) are on the same level and their targets can depend on each other (projects' targets can depend on subprojects' targets, but not vice versa).

但是,如果您的库(子项目)被各种其他项目(或它们的目标,准确地说)使用,则将其置于相同的层次结构级别是有意义的 - 这就是工作区的用途。工作区包含和管理项目,它直接包含的所有项目(即,不是它们的子项目)都在同一级别上,它们的目标可以相互依赖(项目的目标可以依赖于子项目的目标,反之则不然)。

Workspace structure

工作区结构

Workspace structure

工作区结构

In this example, both apps (AnotherApplication/ ProjectStructureExample) can reference the demoLibproject's targets. This would also be possible by including the demoLibproject in both other projects as a subproject (which is a reference only, so no duplication necessary), but if you have lots of cross-dependencies, workspaces make more sense. If you open a workspace, you can choose from all projects' targets when building/running.

在此示例中,两个应用程序(AnotherApplication/ ProjectStructureExample)都可以引用demoLib项目的目标。这也可以通过将demoLib项目作为子项目包含在其他两个项目中(仅供参考,因此无需重复),但如果您有很多交叉依赖关系,工作区更有意义。如果打开工作区,则可以在构建/运行时从所有项目的目标中进行选择。

Running targets from a workspace

从工作区运行目标

You can still open your project files separately, but it is likely their targets won't build because Xcode cannot resolve the dependencies unless you open the workspace file. Workspaces give you the same benefit as subprojects: Once a dependency changes, Xcode will rebuild it to make sure it's up-to-date (although I have had some issues with that, it doesn't seem to work reliably).

您仍然可以单独打开您的项目文件,但它们的目标很可能不会构建,因为 Xcode 无法解析依赖项,除非您打开工作区文件。工作区为您提供与子项目相同的好处:一旦依赖项发生变化,Xcode 将重建它以确保它是最新的(尽管我遇到了一些问题,但它似乎不能可靠地工作)。

Your questions in a nutshell:

你的问题简而言之

1) Projects contain files (code/resouces), settings, and targets that build products from those files and settings. Workspaces contain projects which can reference each other.

1) 项目包含文件(代码/资源)、设置和从这些文件和设置构建产品的目标。工作区包含可以相互引用的项目。

2) Both are responsible for structuring your overall project, but on different levels.

2)两者都负责构建您的整个项目,但在不同的层次上。

3) I think projects are sufficient in most cases. Don't use workspaces unless there's a specific reason. Plus, you can always embed your project in a workspace later.

3)我认为项目在大多数情况下就足够了。除非有特定原因,否则不要使用工作区。此外,您以后可以随时将项目嵌入到工作区中。

4) I think that's what the above text is for…

4)我认为这就是上面的文字是为了……

There's one remark for 3): CocoaPods, which automatically handles 3rd party libraries for you, uses workspaces. Therefore, you have to use them, too, when you use CocoaPods(which a lot of people do).

3) 有一个备注:CocoaPods会自动为您处理 3rd 方库,使用工作区。因此,您在使用时也必须使用它们CocoaPods(很多人都这样做)。

回答by andreamazz

A workspace is a collection of projects. It's useful to organize your projects when there's correlation between them (e.g.: Project A includes a library, that is provided as a project itself as project B. When you build the workspace project B is compiled and linked in project A).
It's common to use a workspace in the popular CocoaPods. When you install your pods, they are placed inside a workspace, that holds your project and the pod libraries.

工作区是项目的集合。当它们之间存在相关性时,组织您的项目很有用(例如:项目 A 包括一个库,该库作为项目本身作为项目 B 提供。当您构建工作区时,项目 B 在项目 A 中被编译和链接)。
在流行的CocoaPods 中使用工作区是很常见的。当您安装 Pod 时,它们被放置在一个工作空间中,该工作空间包含您的项目和 Pod 库。

回答by onmyway133

In brief

简单来说

  • Xcode 3 introduced subproject, which is parent-child relationship, meaning that parent can reference its child target, but no vice versa
  • Xcode 4 introduced workspace, which is sibling relationship, meaning that any project can reference projects in the same workspace
  • Xcode 3 引入了子项目,子项目是父子关系,意味着父可以引用它的子目标,反之则不行
  • Xcode 4 引入了workspace,是兄弟关系,意味着任何项目都可以引用同一个workspace中的项目

回答by ifeegoo

When I used CocoaPods to develop iOS projects, there is a .xcworkspacefile, you need to open the project with .xcworkspacefile related with CocoaPods.

我用CocoaPods开发iOS项目的时候,有一个.xcworkspace文件,需要.xcworkspace用CocoaPods相关的文件打开项目。

Files preview

文件预览

But when you Show Package Contentswith .xcworkspacefile, you will find the contents.xcworkspacedatafile.

但是当您Show Package Contents使用.xcworkspace文件时,您会找到该contents.xcworkspacedata文件。

Package contents

包装内容

<?xml version="1.0" encoding="UTF-8"?>
<Workspace
   version = "1.0">
   <FileRef
      location = "group:BluetoothColorLamp24G.xcodeproj">
   </FileRef>
   <FileRef
      location = "group:Pods/Pods.xcodeproj">
   </FileRef>
</Workspace>

pay attention to this line:

注意这一行:

location = "group:BluetoothColorLamp24G.xcodeproj"

location = "group:BluetoothColorLamp24G.xcodeproj"

The .xcworkspacefile has reference with the .xcodeprojfile.

.xcworkspace文件具有与该.xcodeproj文件的参考。

Development Environment:

开发环境:

macOS 10.14
Xcode 10.1

回答by yoAlex5

  1. What is the difference between the two of them?
    Workspace is a set of projects

  2. What are they responsible for?
    Project is responsible for the source code. Workspace is responsible for dependencies between projects

  3. Which one of them should I work with when I'm developing my Apps in team/alone?
    You choice should depends on a type of your project. For example if your project relies on CocoaPods dependency manager it creates a workspace.

  4. Is there anything else I should be aware of in matter of these two files?
    A competitor of workspace is cross-project references[About]

  1. 他们两个有什么区别?
    工作区是一组项目

  2. 他们负责什么?
    项目负责源代码。Workspace 负责项目之间的依赖

  3. 当我在团队中/单独开发我的应用程序时,我应该与他们中的哪一个一起工作?
    您的选择应该取决于您的项目类型。例如,如果您的项目依赖 CocoaPods 依赖项管理器,它会创建一个工作区。

  4. 关于这两个文件还有什么我应该注意的吗?
    工作空间的竞争对手是cross-project references[关于]

[Xcode components]

[Xcode 组件]