简体   繁体   English

将文件移动到 Xcode 中的真实文件夹中

[英]Moving Files into a Real Folder in Xcode

When I started my project I was happy to use Groups in Xcode rather than literal folders: Since I'm using the browser in Xcode to access everything, stuff was nicely organized and I was happy.当我开始我的项目时,我很高兴在 Xcode 中使用组而不是文字文件夹:因为我在 Xcode 中使用浏览器来访问所有内容,所以东西组织得很好,我很高兴。

However, now that the project is about to be shared for version control, the project folder itself is a horror show for those trying to scan it via a terminal, about 300 files, over half of which are graphics.然而,现在该项目即将共享以进行版本控制,对于那些试图通过终端扫描它的人来说,项目文件夹本身就是一场恐怖秀,大约 300 个文件,其中一半以上是图形。

I'm trying to now reorganize things, creating real folders and importing them into Xcode.我现在正在尝试重新组织事物,创建真正的文件夹并将它们导入 Xcode。 Unfortunately Xcode doesn't let me work with them the way it does with groups.不幸的是,Xcode 不允许我像处理组那样处理它们。 For example, if I right-click on an actual added folder (blue, not yellow) and choose to add existing files, it doesn't actually put them in that folder, it puts them in its root.例如,如果我右键单击一个实际添加的文件夹(蓝色,而不是黄色)并选择添加现有文件,它实际上并没有将它们放入该文件夹中,而是将它们放入其根目录中。

Similarly, I can't move a file from a faux-folder (a group) into a real folder: Xcode doesn't consider the real folders to be valid places to move stuff to.同样,我无法将文件从人造文件夹(一组)移动到真实文件夹中:Xcode 不认为真实文件夹是将内容移动到的有效位置。

What am I missing?我错过了什么? How can I convince Xcode to let me use the folders the way I use groups?我怎样才能说服 Xcode 让我像使用组一样使用文件夹? There's an answer here to a somewhat similar question , but it doesn't actually solve my problem since I'm working with existing files.这里有一个类似问题的答案,但它实际上并没有解决我的问题,因为我正在处理现有文件。

A modern (and dead simple!) approach for 2017 (Xcode 6, 7, 8, and sometimes 9, since it does it automagically some of the time): 2017年的一种现代(简单而死的简单!)方法(Xcode 6,7,8,有时9,因为它在某些时候自动完成):

If you're moving a bunch of files into a new folder and are keeping the child hierarchy, it's actually a lot easier than moving each file individually: 如果您将一堆文件移动到一个新文件夹并保持子层次结构,那么实际上比单独移动每个文件要容易得多:

  1. Create new groups in the Xcode folder tree and organize your files into them however you like. 在Xcode文件夹树中创建新组,并根据需要将文件组织到它们中。
  2. Create a matching physical folder tree in Finder and organize your physical files into them to match what you did in step 1. 在Finder中创建匹配的物理文件夹树,并将物理文件组织到其中,以匹配您在步骤1中执行的操作。
  3. All the references in Xcode should now be red (that's OK!). Xcode中的所有引用现在都应该是红色的(没关系!)。
  4. From the Identity and Type manager, select the Group in Xcode that you want to relocate, then click the folder icon from the info pane: 从身份和类型管理器中,选择要重定位的Xcode中的 ,然后单击信息窗格中的文件夹图标:

  5. In the Finder selection dialog, locate the equivalent new folder you created for this group in step 2. All the files inside that group will now be automagically rediscovered! 在Finder选择对话框中,找到您在步骤2中为该组创建的等效新文件夹。该组中的所有文件现在将自动重新发现!

Isn't that nice? 不是很好吗? At most you'll have to repeat these 5 steps once for each new group you've created (which beats relocating each file individually!) 对于您创建的每个新组,最多只需要重复这5个步骤(这样可以单独重新定位每个文件!)

Bonus Points! 奖励积分!

Say you accidentally screwed up the move and now a bunch of your files are red and can't be found: select multiple files that are broken, and using the same folder icon in the screenshot from step 4, find the correct folder that contains these files and they'll automatically resolve the missing paths. 假设您不小心搞砸了移动,现在一堆文件都是红色的,无法找到:选择多个已损坏的文件,并在步骤4的屏幕截图中使用相同的文件夹图标,找到包含这些文件的正确文件夹文件,他们将自动解决丢失的路径。

FURTHER EDITED JUNE 2017: Xcode 9 does this automatically, no special effort required. 2017年6月进一步编辑: Xcode 9自动执行此操作,无需特殊操作。 This answer and Brandon's only apply to Xcode 8 and earlier. 这个答案和Brandon仅适用于Xcode 8及更早版本。

EDITED DECEMBER 2016: Brandon's answer below is a better solution now. 2016年12月编辑: Brandon的答案现在是一个更好的解决方案。 Back when this answer was created in 2010 this was the only option I could find. 回到2010年创建这个答案时,这是我能找到的唯一选择。 I now suggest Brandon's answer , below. 我现在建议布兰登的答案如下。


It turns out that moving files into real folders is certainly possible, though not as simple as it should be. 事实证明,将文件移动到真实文件夹当然是可能的,尽管不是应该如此简单。

I got the basic information from a question here, Xcode organising files and folders (core data model objects - iPhone) , but learned important things along the way. 我从这里得到了一个问题的基本信息, Xcode组织文件和文件夹(核心数据模型对象 - iPhone) ,但在此过程中学到了重要的东西。

The Process 过程

Moving the files is a two-step process with multiple sub-steps: 移动文件分为两步,包括多个子步骤:

  1. Tell Xcode where you want the files to be : 告诉Xcode你想要文件的位置

    • Right/Control-click on the file or file group that you'd like to move and choose Get Info from the contextual menu that appears. 右键/按住Control键并单击要移动的文件或文件组,然后从显示的上下文菜单中选择“ 获取信息 ”。 The Group Info or File Info window appears. 将出现“ 组信息”或“ 文件信息”窗口。

    • Click the Choose button on the far right side of the window in the Path area. 单击“ 路径”区域中窗口最右侧的“选择”按钮。 A dialog box appears. 出现一个对话框。

    • Navigate to the folder you want the files to be moved to. 导航到要将文件移动到的文件夹。 Create a New Folder if needed. 如果需要,创建一个新文件夹 Click the Choose button in the bottom-right corner of the dialog box, then close the Group Info/File Info window. 单击对话框右下角的“ 选择”按钮,然后关闭“ 组信息/文件信息”窗口。

    • The names of the file/files in the group will turn red to indicate that Xcode can't find them in the place you specified. 组中文件/文件的名称将变为红色,表示Xcode无法在您指定的位置找到它们。

  2. Move the actual files 移动实际文件

    • In the Finder (or Git) move the files you selected in step 1 into the actual folders you want them in. 在Finder(或Git)中,将您在步骤1中选择的文件移动到所需的实际文件夹中。

    • Switch back to Xcode. 切换回Xcode。 The files/groups should all have turned black again. 文件/组应该再次变黑。 If any are still red then you've missed moving something to the right folder. 如果有任何仍然是红色,那么你错过了移动到正确的文件夹。

Tips 提示

I learned a couple of important things while adjusting the ~300 files in this project: 在调整此项目中的~300个文件时,我学到了几个重要的事情:

  • Some files refuse to move this way; 有些文件拒绝这样移动; that is, when you navigate to the new destination the Choose button is disabled, as is the New Folder button. 也就是说,当您导航到新目标时, “选择”按钮将被禁用,“ 新建文件夹”按钮也将被禁用。 The solution, though I don't know why it makes a difference, is to first use Xcode to put those files in a Group (right/control-click the files and choose Group , and give the group a name) and then move the group to the new location. 解决方案,虽然我不知道为什么会有所不同,但是首先使用Xcode将这些文件放在一个组中(右键/控制 - 单击文件并选择 ,并为组命名)然后移动分组到新位置。 After you're actually moved the files in the Finder you can remove them from the group (by dragging them into the new parent group/folder and deleting the group). 在实际移动Finder中的文件后,您可以将它们从组中删除(通过将它们拖到新的父组/文件夹中并删除该组)。

  • Stop and build every few minutes, after completing step 2 for a number of files. 完成步骤2后,每隔几分钟就停止并构建一些文件。 The build will tell you if you've screwed anything up so far, making it easier to go back and fix it before you've done too much damage. 构建将告诉你到目前为止你是否搞砸了任何东西,在你造成太多伤害之前更容易回去修复它。

  • If the files won't move to where you want them to — I had a devil of a time moving some files that had been created early-on in the Classes folder — you can simply drag them out of their old place in the finder to someplace handy like the desktop, delete references to them in Xcode, and then re-import them via the right/control-click Add Existing Files option. 如果文件不会移动到你想要的位置 - 我有一段时间移动一些早期在Classes文件夹中创建的文件 - 你可以简单地将它们从查找器中的旧位置拖出像桌面一样方便,在Xcode中删除对它们的引用,然后通过右键/控制单击“ 添加现有文件”选项重新导入它们。

  • If in the past you've used Xcode to delete references to files without also moving them to the trash in this project, you'll find files that don't have to be moved but at just sitting there. 如果在过去你使用Xcode删除对文件的引用而不将它们移动到这个项目的垃圾箱中,你会发现不必移动但只是坐在那里的文件。 Be careful that you don't do what I did, confusing the names of a current group of files I was moving and the older, no-longer-linked files, insisting that Xcode import them because you thought it was being dense. 小心你不做我做的事情,混淆我正在移动的当前文件组的名称和旧的,不再链接的文件,坚持Xcode导入它们因为你认为它是密集的。

Hope this helps some future person. 希望这有助于未来的人。 I'm surprised that it was as difficult and occasionally tricky as it was. 令我感到惊讶的是,它既困难又偶尔也很棘手。

In Xcode 5 or Xcode 6: 在Xcode 5或Xcode 6中:

  1. Create the folders that map to your Groups in Finder 在Finder中创建映射到您的组的文件夹
  2. Move the files into those folders in Finder 将文件移动到Finder中的那些文件夹中
  3. Select each file that is red in the Xcode sidebar on the left 在左侧的Xcode侧栏中选择红色的每个文件
  4. Click the button "Show/Hide Utilities" to reveal the right sidebar (see figure) 单击“显示/隐藏实用程序”按钮以​​显示右侧边栏(参见图)
  5. In "Identity and Type", click the tiny button and select the file location (see figure) 在“身份和类型”中,单击微小按钮并选择文件位置(参见图)

在此输入图像描述

Cheers. 干杯。

I've found the most reliable way to work around XCode's appalling design here is to organize ALL your files in Finder, for two reasons: 我发现解决XCode令人震惊的设计最可靠的方法是在Finder中整理所有文件,原因有两个:

  1. Finder doesn't make "mistakes", unlike XCode's GUI 与XCode的GUI不同,Finder不会出现“错误”
  2. Once things are organized in Finder, you can drag/drop entire Finder folders into XCode, and ... it does exactly what you wanted, with no effort 在Finder中组织完成后,您可以将整个Finder文件夹拖放到XCode中,并且......它完全按照您的要求进行操作,不费力气

Even when moving existing XCode files around, it is quicker to do this: 即使在移动现有的XCode文件时,也可以更快地执行此操作:

  1. Select the files in Finder 在Finder中选择文件
  2. Create a folder in Finder for them 在Finder中为它们创建一个文件夹
  3. Drag/drop them to the folder (automatically Moves them) 将它们拖放到文件夹(自动移动它们)
  4. Drag/drop the Finder folder into Xcode (automatically: creates the Xcode folder, adds every file in the Finder folder) 将Finder文件夹拖放到Xcode中(自动:创建Xcode文件夹,添加Finder文件夹中的每个文件)
  5. cmd-select every "red" file you now see in Xcode (because you moved them) and hit the delete key cmd - 选择您现在在Xcode中看到的每个“红色”文件(因为您移动了它们)并点击删除键

NB: I never use the "create dragged folders with sub-groups" option, because in XCode 3 that was often hopelessly buggy and could corrupt projects. 注意:我从不使用“创建带有子组的拖动文件夹”选项,因为在XCode 3中,它通常是无可救药的错误并且可能损坏项目。 Apple clearly (IMHO) does NOT use this feature internally, otherwise it would never have been allowed to be so buggy; 苹果显然(恕我直言)不会在内部使用此功能,否则它将永远不会被允许如此错误; if Apple doesn't use a thing, it's generally not safe to use it either - they aren't good at testing :) 如果Apple没有使用它,它通常也不安全使用它 - 它们不擅长测试:)

There is a simple to setup and use Command Line Tool - "synx" available in github that do exactly what is needed here. 有一个简单的设置和使用命令行工具 - github中提供的“synx”完全符合此处所需。

It reorganizes Xcode project folder in finder to match Xcode groups in project. 它在finder中重新组织Xcode项目文件夹以匹配项目中的Xcode组。

You can find it here: https://github.com/venmo/synx 你可以在这里找到它: https//github.com/venmo/synx

UPDATE: XCode 9 supports this feature by default. 更新: XCode 9默认支持此功能。 So, no need to use other tools anymore! 所以,不再需要使用其他工具了!

Xcode 9 Xcode 9

It seems Xcode 9 now supports it by default. 似乎Xcode 9现在默认支持它。 When you move files from a group to another, the file will also be moved from the old folder to the new folder. 将文件从一个组移动到另一个组时,该文件也将从旧文件夹移动到新文件夹。 This was announced in WWDC 2017. 这是在2017年WWDC上宣布的。

I used the following tool to achieve it. 我使用以下工具来实现它。 Organize Folders in Groups Xcode 组织组Xcode中的文件夹

--no-default-exclusions

I'm usually move files to the directory directly in the Finder, then fix the files with red-color names in XCode in their "Get Info" dialog: click the "Choose..." button and select file's new locatoin. 我通常直接在Finder中将文件移动到目录中,然后在“获取信息”对话框中修复XCode中带有红色名称的文件:单击“选择...”按钮并选择文件的新locatoin。 That's the first response when I want to move files in XCode, and it works. 这是我想在XCode中移动文件的第一个响应,它可以工作。

As of Xcode 4.5 when you drag in a folder structure it is automatically turned into nested groups in the Project Navigator. 从Xcode 4.5开始,当您在文件夹结构中拖动时,它会自动转换为Project Navigator中的嵌套组。 Then, if you look on disk (eg right click and choose Show in Finder), the folder structure has been retained. 然后,如果您在磁盘上查看(例如,右键单击并选择“在Finder中显示”),则保留文件夹结构。

I found trying to fix things from an older project was just a pain. 我发现尝试从旧项目中解决问题只是一种痛苦。 It turned out much easier to just delete these files and drag them in again. 事实证明,删除这些文件并再次将它们拖入更容易。

To move a folder in xcode 4.5 I just... 要在xcode 4.5中移动文件夹我只是...

  1. Delete the files/groups from xcode and select "Remove Reference". 从xcode中删除文件/组,然后选择“删除参考”。
  2. Go into finder and move the folder/files as needed. 进入取景器并根据需要移动文件夹/文件。
  3. Once done I go back into Xcode and choose File->Add Files to {ProjectName}. 完成后,我回到Xcode并选择File-> Add Files to {ProjectName}。
  4. Make sure "Copy Items into Destination Group's folder (If needed)" is checked 确保选中“将项目复制到目标组的文件夹(如果需要)”
  5. Make sure "Create Group for any added folder" is checked 确保选中“为任何添加的文件夹创建组”
  6. Make sure "Add to Target" is checked for your project 确保为项目选中“添加到目标”

Easiest technique for XCode 8.... 最简单的XCode 8技术....

Assuming you have files A,B,C in a logical group, but want them moved into a folder on your hard drive. 假设您在逻辑组中有文件A,B,C,但希望将它们移动到硬盘驱动器上的文件夹中。

  1. Create a destination folder via Finder 通过Finder创建目标文件夹
  2. Add the folder in XCode (File -> Add Files ) using the Options pane to select a Folder Reference (not a group) 使用“选项”窗格在XCode(文件 - >添加文件)中添加文件夹以选择文件夹参考(不是组)
  3. Drag files A,B,C from the (old) group to the (new) folder reference in the project navigator. 将文件A,B,C从(旧)组拖到项目导航器中的(新)文件夹引用。 XCode will move the files into the folder, both in the the project, and on disk. XCode会将文件移动到项目中和磁盘上的文件夹中。

Done. 完成。

On XCode 11 (I just validated this on XCode 11.6), you can select the files you would like to move under the project navigator, right click on the selected files, and click "New Group From Selection".在 XCode 11 上(我刚刚在 XCode 11.6 上验证了这一点),您可以在项目导航器下选择要移动的文件,右键单击所选文件,然后单击“New Group From Selection”。 This will create a folder on disk and move the files appropriately.这将在磁盘上创建一个文件夹并适当地移动文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM