XCode、文件夹和#import 语句

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

XCode, folders and #import statement

xcodeimportrelative-pathdirectory

提问by Dmitriy

I'm writing an app for the iOS in XCode 4 and stumbled upon an issue with #import statement.

我正在使用 XCode 4 为 iOS 编写应用程序,并偶然发现了 #import 语句的问题。

All my source files are organized in folders, every folder is mapped to the group or subgroup in XCode. Consider the following:

我所有的源文件都组织在文件夹中,每个文件夹都映射到 XCode 中的组或子组。考虑以下:

ProjectFolder

项目文件夹

-SubFolderA

-子文件夹A

--FileA.h

--文件A.h

-SubFolderB

-子文件夹B

--FileB.m

--文件B.m

Usually, if I want to import FileA into FileB, I write #import "FileA.h", and it works perfectly. But sometimes I have to write relative path, like #import "../SubFolderA/FileA.h". What should I do to be always able to write only name of the file?

通常,如果我想将 FileA 导入到 FileB 中,我会写#import "FileA.h",它可以完美运行。但有时我必须写相对路径,比如#import "../SubFolderA/FileA.h". 我应该怎么做才能始终只写文件名?

This behavior seems undetermined to me.

这种行为对我来说似乎不确定。

PS: I don't want to keep all this mess in one root folder.

PS:我不想把所有这些乱七八糟的东西放在一个根文件夹中。

PS#2: Yes, I know how the 'folder' differs from 'group'. I've created folders on the early stage of the project, dragged them from Finder to XCode and mapped them to groups. And everything worked just fine - I was able to import headers using only filenames.

PS#2:是的,我知道“文件夹”与“组”有何不同。我在项目的早期阶段创建了文件夹,将它们从 Finder 拖到 XCode 并将它们映射到组。一切都很好 - 我只能使用文件名导入标题。

采纳答案by ennuikiller

You can always set the "Header Search Paths" under build options to specify which directories to search in. Don't confuse folders and groups though. They are completely independent. In particular groups are simply a convenience that allow you to organize your files within Xcode.

您始终可以在构建选项下设置“标题搜索路径”以指定要搜索的目录。但不要混淆文件夹和组。他们是完全独立的。特别是组只是一种方便,允许您在 Xcode 中组织您的文件。

回答by Husyn

Search "Scan All Source Files for Includes" in your project's and target's build settings and change the value from "NO" to "Yes". It will solve this issue.

在项目和目标的构建设置中搜索“Scan All Source Files for Includes”,并将值从“NO”更改为“Yes”。它将解决这个问题。

回答by Nicholas Smith

XCode is very much like iTunes in some respects, it wants to manage your files and hide them from you. When you create a group in XCode it's appearance is that of a 'folder' but if you go into the actual project folder you'll still see files everywhere.

XCode 在某些方面与 iTunes 非常相似,它想管理您的文件并向您隐藏它们。当你在 XCode 中创建一个组时,它的外观是一个“文件夹”,但如果你进入实际的项目文件夹,你仍然会看到到处都是文件。