如何将现有的源代码文件正确添加到我的 Xcode 项目中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2883506/
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-14 19:22:41  来源:igfitidea点击:

How do I properly add existing source code files to my Xcode project?

iphonexcode

提问by BeachRunnerFred

I'm new to iPhone development and I'm still getting familiar with the Mac dev environment, including Xcode. I want to add some 3rd party code to my iPhone project, but when I add the "existing files" to my Xcode project, I'm presented with a dialog box that has far too many options that I don't understand and, as such, my project isn't working. When I #import headerfilename.h, I get a build error that reads headerfilename.h: No such file or directory.

我是 iPhone 开发的新手,我仍然熟悉 Mac 开发环境,包括 Xcode。我想向我的 iPhone 项目添加一些 3rd 方代码,但是当我将“现有文件”添加到我的 Xcode 项目时,我会看到一个对话框,其中有太多我不理解的选项,并且,这样,我的项目不起作用。当 I 时#import headerfilename.h,我收到一个构建错误,内容为headerfilename.h: No such file or directory.

alt text http://joecrotchett.com/images/misc/fileadd.jpg

替代文字 http://joecrotchett.com/images/misc/fileadd.jpg

  1. Can anyone explain to me what all these options mean or give me a link to some documentation that can? I'm having a hard time finding anything in Apple's docs.
  2. Which options do I want to choose to add existing source code files to my Xcode project? I should note that the source code files that I'm trying to add are located in my project/Classes/frameworkname/ directory.
  3. After they're added, do I need to reference this new code directory in my project settings anywhere (i.e. some kind of header file directory variable)?
  1. 任何人都可以向我解释所有这些选项的含义,或者给我一个可以链接到一些文档的链接?我很难在 Apple 的文档中找到任何内容。
  2. 我想选择哪些选项来将现有的源代码文件添加到我的 Xcode 项目中?我应该注意到,我尝试添加的源代码文件位于我的 project/Classes/frameworkname/ 目录中。
  3. 添加它们后,我是否需要在我的项目设置中的任何地方引用这个新的代码目录(即某种头文件目录变量)?

Thanks so much!

非常感谢!

Update:I found the following answers/responses on the apple dev forums that were very useful and helped me fix my issue...

更新:我在苹果开发论坛上找到了以下非常有用的答案/回复,并帮助我解决了我的问题......

To make it simple : - if you do not check the copy option, the file stay where it is. - if you check it, it is copied in your project folders In the first case (what it seems you are doing) you need to tell the compiler that the header files are in another directory : - project info -> build -> search paths -> User Header Search Path : add the directory from where you took the header file Hope this will help

简单起见: - 如果您不选中复制选项,文件将保留在原处。- 如果你检查它,它会被复制到你的项目文件夹中 在第一种情况下(你似乎在做什么)你需要告诉编译器头文件在另一个目录中: - 项目信息 -> 构建 -> 搜索路径-> 用户头搜索路径:添加您获取头文件的目录希望这会有所帮助



You have discovered the most confusing dialog box that ever came out of Cupertino. Six years of Xcode, and this thing still is partly a mystery to me. To even get that far, I had to make many test projects to try and reverse-engineer what this thing does. The "Copy" box means that it will copy the files as they are right now, into the project. If this box is not checked, then it just references those files during a build and copies them as they are at THAT time. For source code, you want the Copy box checked.
The "relative to" is a total mystery to me and I can't help you with that. I usually leave it however it is already set. Does it mean relative to where they are on disk, or the arrangement in Xcode, or in the bundle? Who knows. The last 2 radio buttons SEEM to mean that it will either re-create the folder structure of the folder you are adding, or just put "fake" folders in Xcode that point to the real folders. This is probably your problem - you are adding source code that is not all at the top level, and when it goes to find it, it does not re-create the hierarchy. Others can supply a better way, hopefully, but what I would do is put all of the source in one folder and add that, using the Copy box. Then in Xcode you can make whatever bogus folders you want and put the source file names in those fake folders.

您已经发现了库比蒂诺有史以来最令人困惑的对话框。Xcode 六年了,这件事对我来说仍然部分是个谜。为了做到这一点,我不得不制作许多测试项目来尝试对这件事的功能进行逆向工程。“复制”框意味着它将把文件按原样复制到项目中。如果未选中此框,则它只会在构建期间引用这些文件,并按当时的原样复制它们。对于源代码,您希望选中复制框。
“相对于”对我来说完全是个谜,我无能为力。我通常会离开它,但它已经设置好了。这是否意味着相对于它们在磁盘上的位置,或者在 Xcode 中的排列,或者在包中?谁知道。最后 2 个单选按钮似乎意味着它将重新创建您正在添加的文件夹的文件夹结构,或者只是在 Xcode 中放置指向真实文件夹的“假”文件夹。这可能是您的问题 - 您添加的源代码并非全部位于顶级,并且当它找到它时,它不会重新创建层次结构。希望其他人可以提供更好的方法,但我要做的是将所有源放在一个文件夹中,然后使用“复制”框添加它。

采纳答案by vodkhang

Copy items into destination group's folder (if needed)

将项目复制到目标组的文件夹(如果需要)

Well, only if you want it to copy the files. If you are happy with their existing location and don't want it to copy them, don't select it (that's what I do).

好吧,仅当您希望它复制文件时。如果您对他们现有的位置感到满意并且不希望它复制它们,请不要选择它(这就是我所做的)。

Reference Type:

参考类型:

A total mystery to me

对我来说完全是个谜

Text Encoding

文本编码

Self explanatory

不言自明

Recursively create groups for any added folders Create Folder References for any added folders

为任何添加的文件夹递归创建组为任何添加的文件夹创建文件夹引用

Leave this as the default and then organize the folders in your Xcode project any way you want.

将此保留为默认值,然后以任何您想要的方式组织 Xcode 项目中的文件夹。

Add to targets

添加到目标

If you add an additional target to your project, e.g. for iPhone vs. iPad versions, then not noticing that it has by default added the files to just one of them (at random?) can cause headaches.

如果您向项目添加额外的目标,例如 iPhone 与 iPad 版本,则没有注意到默认情况下它仅将文件添加到其中一个(随机?)可能会导致头痛。

Also, make sure you add the new header directory to your search paths in Project Settings > Build > Search paths. I think that's what's causing your problem.

此外,请确保将新的头目录添加到项目设置 > 构建 > 搜索路径中的搜索路径中。我认为这就是导致您出现问题的原因。

Hope this helps, I remember how confusing this was to me at first.

希望这会有所帮助,我记得起初这对我来说是多么的困惑。

回答by Sam Clewlow

This is from the Xcode user guide:

这是来自 Xcode 用户指南:

"The project navigator shows projects, groups, folders, and files:

The project or projects in your workspace window are the highest level of the hierarchy in the project navigator. Open the project's disclosure triangle to see the groups, folders, and files in the project. Select the project to display the project editor, where you can view and edit project and target settings.

A group appears in the project navigator as a yellow folder icon. The group does not represent a folder on disk. Although you can organize your project in Xcode to reflect the organization of files on disk, moving files into and out of groups does not affect the content of the folders on disk, and moving files on disk into and out of folders does not affect the content of the groups in the project navigator.

A folder is a reference to a folder on disk and appears in the project navigator as a blue folder icon. The contents of the folder in the project navigator reflect the contents of the folder on disk. It's important to note, however, that the files that appear in a folder icon in the project navigator are not part of your project unless you have added them to the project explicitly (in which case they appear directly under the project or in a group, as well as in the folder). You can view and edit the files in a folder, but to move files in and out of the folder you must use the Finder.

A file in the project navigator is a reference to a file on disk."

“项目导航器显示项目、组、文件夹和文件:

工作区窗口中的一个或多个项目是项目导航器中层次结构的最高级别。打开项目的显示三角形以查看项目中的组、文件夹和文件。选择项目以显示项目编辑器,您可以在其中查看和编辑项目和目标设置。

一个组在项目导航器中显示为黄色文件夹图标。该组不代表磁盘上的文件夹。虽然您可以在 Xcode 中组织您的项目以反映磁盘上文件的组织方式,但将文件移入和移出组不会影响磁盘上文件夹的内容,将磁盘上的文件移入和移出文件夹不会影响内容项目导航器中的组。

文件夹是对磁盘上文件夹的引用,在项目导航器中显示为蓝色文件夹图标。项目导航器中文件夹的内容反映了磁盘上文件夹的内容。但是,请务必注意,出现在项目导航器中文件夹图标中的文件不是项目的一部分,除非您已明确将它们添加到项目中(在这种情况下,它们直接出现在项目下或组中,以及在文件夹中)。您可以查看和编辑文件夹中的文件,但要将文件移入和移出文件夹,您必须使用 Finder。

项目导航器中的文件是对磁盘上文件的引用。”

回答by vodkhang

I think usually the default option is the best one. If you want to add into some group, you can tick on the "Copy Item to Destination group"

我认为通常默认选项是最好的。如果要添加到某个组中,可以勾选“将项目复制到目标组”

You don't have to change any project settings after adding

添加后您无需更改任何项目设置

And if you want to add the whole framework code, please choose add existing framework

如果要添加整个框架代码,请选择添加现有框架

回答by cem

If you want to add an Framwork Bundle, just add is as one. Add -> Existing Frameworks

如果你想添加一个框架包,只需添加一个。 Add -> Existing Frameworks