复制并重命名 Xcode 项目和相关文件夹

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

Duplicate and rename Xcode project & associated folders

xcodeproject-management

提问by Sammio2

I would like to be able to duplicate an entire Xcode project, rename the project and the associated files without Xcode going mental, and without having to manually re-import all files to the project.

我希望能够复制整个 Xcode 项目,重命名项目和相关文件,而无需 Xcode 思考,也无需手动将所有文件重新导入项目。

回答by Sammio2

This answer is the culmination of various other StackOverflow posts and tutorials around the internet brought into one place for my future reference, and to help anyone else who may be facing the same issue. All credit is given for other answers at the end.

这个答案是互联网上各种其他 StackOverflow 帖子和教程的高潮,汇集到一个地方供我将来参考,并帮助可能面临相同问题的其他人。最后给出了其他答案的所有功劳。

Duplicating an Xcode Project

复制 Xcode 项目

  • In the Finder, duplicate the project folder to the desired location of your new project. Do not rename the .xcodeproj file name or any associated folders at this stage.

  • In Xcode, rename the project. Select your project from the navigator pane (left pane). In the Utilities pane (right pane) rename your project, Accept the changes Xcode proposes.

  • In Xcode, rename the schemes in "Manage Schemes", also rename any targets you may have.

  • If you're not using the default Bundle Identifier which contains the current PRODUCT_NAME at the end (so will update automatically), then change your Bundle Identifier to the new one you will be using for your duplicated project.

  • 在 Finder 中,将项目文件夹复制到新项目的所需位置。在此阶段不要重命名 .xcodeproj 文件名或任何关联的文件夹。

  • 在 Xcode 中,重命名项目。从导航器窗格(左窗格)中选择您的项目。在 Utilities 窗格(右窗格)中重命名您的项目,接受 Xcode 建议的更改。

  • 在 Xcode 中,重命名“管理方案”中的方案,同时重命名您可能拥有的任何目标。

  • 如果您没有使用最后包含当前 PRODUCT_NAME 的默认捆绑标识符(因此会自动更新),请将捆绑标识符更改为您将用于复制项目的新捆绑标识符。

Renaming the source folder

重命名源文件夹

So after following the above steps you should have a duplicated and renamed Xcode project that should build and compile successfully, however your source code folder will still be named as it was in the original project. This doesn't cause any compiler issues, but it's not the clearest file structure for people to navigate in SCM, etc. To rename this folder without breaking all your file links, follow these steps:

因此,在执行上述步骤后,您应该有一个复制并重命名的 Xcode 项目,该项目应该可以成功构建和编译,但是您的源代码文件夹仍将命名为原始项目中的名称。这不会导致任何编译器问题,但它不是人们在 SCM 等中导航的最清晰的文件结构。要在不破坏所有文件链接的情况下重命名此文件夹,请执行以下步骤:

  • In the Finder, rename the source folder. This will break your project, because Xcode won't automatically detect the changes. All of your xcode file listings will lose their links with the actual files, so will all turn red.

  • In Xcode, click on the virtual folder which you renamed (This will likely be right at the top, just under your actual .xcodeproject) Rename this to match the name in the Finder, this won't fix anything and strictly isn't a required step but it's nice to have the file names matching.

  • In Xcode, Select the folder you just renamed in the navigation pane. Then in the Utilities pane (far right) click the icon that looks like dark grey folder, just underneath the 'Location' drop down menu. From here, navigate to your renamed folder in the finder and click 'Choose'. This will automagically re-associate all your files, and they should no longer appear red within the Xcode navigation pane.

  • 在 Finder 中,重命名源文件夹。这会破坏您的项目,因为 Xcode 不会自动检测更改。您所有的 xcode 文件列表都将失去与实际文件的链接,因此都会变成红色。

  • 在 Xcode 中,单击您重命名的虚拟文件夹(这可能就在顶部,就在您实际的 .xcodeproject 下)重命名它以匹配 Finder 中的名称,这不会修复任何内容,严格来说不是必需的步骤,但很高兴让文件名匹配。

  • 在 Xcode 中,在导航窗格中选择您刚刚重命名的文件夹。然后在“实用工具”窗格(最右侧)中,单击“位置”下拉菜单下方看起来像深灰色文件夹的图标。从这里,导航到查找器中重命名的文件夹,然后单击“选择”。这将自动重新关联您的所有文件,并且它们不应再在 Xcode 导航窗格中显示为红色。

Icon to click

点击图标

  • In your project / targets build settings, search for the old folder name and manually rename any occurrences you find. Normally there is one for the prefix.pch and one for the info.plist, but there may be more.

  • If you are using any third party libraries (Testflight/Hockeyapp/etc) you will also need to search for 'Library Search Paths' and rename any occurrences of the old file name here too.

  • Repeat this process for any unit test source code folders your project may contain, the process is identical.

  • 在您的项目/目标构建设置中,搜索旧文件夹名称并手动重命名您找到的任何事件。通常有一个用于 prefix.pch 和一个用于 info.plist,但可能有更多。

  • 如果您使用任何第三方库(Testflight/Hockeyapp/etc),您还需要搜索“库搜索路径”并在此处重命名任何出现的旧文件名。

  • 对您的项目可能包含的任何单元测试源代码文件夹重复此过程,过程是相同的。

This should allow you to duplicate & rename an xcode project and all associated files without having to manually edit any xcode files, and risk messing things up.

这应该允许您复制和重命名 xcode 项目和所有相关文件,而无需手动编辑任何 xcode 文件,并冒着搞砸的风险。

Credits

学分

Many thanks is given to Nick Lockwood, and Pauly Glottfor providing the separate answers to this problem.

非常感谢Nick LockwoodPauly Glott为这个问题提供了单独的答案。

回答by fs_tigre

I'm posting this since I have always been struggling when renaming a project in XCode.

我发布这个是因为我在XCode.

Renaming the project is good and simple but this doesn't rename the source folder. Here is a step by step of what I have done that worked great in Xcode 4 and 5 thanks to the links below.

重命名项目既好又简单,但这不会重命名源文件夹。由于以下链接,这是我在 Xcode 4 和 5 中所做的工作的分步说明。

REF links:
Rename Project.
Rename Source Folder and other files.

REF 链接:
重命名项目。
重命名源文件夹和其他文件。

1- Backup your project.

1-备份您的项目。

If you are using git, commit any changes, make a copy of the entire project folder and backup in time machine before making any changes (this step is not required but I highly recommended).

如果您正在使用 git,请提交任何更改,在进行任何更改之前制作整个项目文件夹的副本并备份在时间机器中(这一步不是必需的,但我强烈建议)。

2- Open your project.

2- 打开您的项目。

3-Slow double click or hit enter on the Project name (blue top icon) and rename it to whatever you like.

3-缓慢双击或点击项目名称(蓝色顶部图标)并将其重命名为您喜欢的任何名称。

NOTE:After you rename the project and press ‘enter' it will suggest to automatically change all project-name-related entries and will allow you to de-select some of them if you want. Select all of them and click ok.

注意:重命名项目并按“输入”后,它会建议自动更改所有与项目名称相关的条目,并允许您根据需要取消选择其中的一些条目。选择所有这些,然后单击确定。

4- Rename the Scheme

4- 重命名方案

a) Click the menu right next to the stop button and select Manage Schemes.

a) 单击停止按钮旁边的菜单并选择管理方案。

b) Single-slow-click or hit enter on the old name scheme and rename it to whatever you like.

b) 在旧名称方案上单击或按 Enter 键,然后将其重命名为您喜欢的任何名称。

c) Click ok.

c) 单击确定。

5 - Build and run to make sure it works.

5 - 构建并运行以确保其正常工作。

NOTES:At this point all of the important project files should be renamed except the comments in the classes created when the project was created nor the source folder. Next we will rename the folder in the file system.

注意:此时所有重要的项目文件都应该重命名,除了在创建项目时创建的类中的注释和源文件夹。接下来,我们将重命名文件系统中的文件夹。

6- Close the project.

6- 关闭项目。

7- Rename the main and the source folder.

7- 重命名主文件夹和源文件夹。

8-Right click the project bundle .xcodeprojfile and select “Show Package Contents” from the context menu. Open the .pbxproj file with any text editor.

8-右键单击项目包.xcodeproj文件并从上下文菜单中选择“显示包内容”。使用任何文本编辑器打开 .pbxproj 文件。

9-Search and replace any occurrence of the original folder name with the new folder name.

9-搜索并用新文件夹名称替换任何出现的原始文件夹名称。

10- Save the file.

10- 保存文件。

11- Open XCode project, test it.

11- 打开 XCode 项目,测试它。

12- Done.

12-完成。

EDIT 10/11/19:

编辑 10/11/19:

There is a tool to rename projects in Xcode I haven't tried it enough to comment on it. https://github.com/appculture/xcode-project-renamer

有一个工具可以在 Xcode 中重命名项目,我还没有尝试过,无法对其进行评论。 https://github.com/appculture/xcode-project-renamer

回答by PBICS

As of XCode 7 this has become much easier.

从 XCode 7 开始,这变得更加容易。

Apple has documented the process on their site: https://developer.apple.com/library/ios/recipes/xcode_help-project_editor/RenamingaProject/RenamingaProject.html

Apple 在其网站上记录了该过程:https: //developer.apple.com/library/ios/recipes/xcode_help-project_editor/RenamingaProject/RenamingaProject.html

Update: XCode 8 link: http://help.apple.com/xcode/mac/8.0/#/dev3db3afe4f

更新:XCode 8 链接:http: //help.apple.com/xcode/mac/8.0/#/dev3db3afe4f

回答by ytbryan

I am using this script after I renamemy iOS Project. It helps to change the directories name and make the names in sync.

我在重命名我的 iOS 项目后使用这个脚本。它有助于更​​改目录名称并使名称同步。

http://github.com/ytbryan/rename

http://github.com/ytbryan/rename

NOTE:you will need to manually change the scheme's name.

注意:您需要手动更改方案的名称。

回答by Giorgio Tempesta

In the example code for the book "Instant OpenCV for iOS"I have found a bash script that copies a project from a folder to another.
Doing a little research I've found a blog post from what seems to be the original author of the script: http://mohrt.blogspot.it/2009/01/renaming-xcode-project-from-command.html, where you can download the script. I gave it a try and running it from terminal like this

“Instant OpenCV for iOS”一书的示例代码中,我找到了一个 bash 脚本,可以将项目从一个文件夹复制到另一个文件夹。
做了一点研究,我发现了一篇似乎是脚本原作者的博客文章:http: //mohrt.blogspot.it/2009/01/renaming-xcode-project-from-command.html,其中你可以下载脚本。我试了一下,像这样从终端运行它

    sh renameXcodeProject.sh <name-of-existing-folder> <name-of-folder-to-create>   

works fine.
Additional info can be found opening the file with a text editor. Hope that helps

工作正常。
使用文本编辑器打开文件可以找到其他信息。希望有帮助

回答by Anton Malmygin

I'm using simple BASH script for renaming.

我正在使用简单的 BASH 脚本进行重命名。

Usage: ./rename.sh oldName newName

用法: ./rename.sh oldName newName

#!/bin/sh

OLDNAME=
NEWNAME=

export LC_CTYPE=C 
export LANG=C
find . -type f ! -path ".*/.*" -exec sed -i '' -e "s/${OLDNAME}/${NEWNAME}/g" {} +

mv "${OLDNAME}.xcodeproj" "${NEWNAME}.xcodeproj"
mv "${OLDNAME}" "${NEWNAME}"

Notes:

笔记:

  1. This script will ignore all files like .gitand .DS_Store
  2. Will not work if old name/new name contains spaces
  3. May not work if you use pods (not tested)
  4. Scheme name will not be changed (anyway project runs and compiles normally)
  1. 此脚本将忽略所有文件,例如.git.DS_Store
  2. 如果旧名称/新名称包含空格将不起作用
  3. 如果您使用 pods(未测试),可能无法工作
  4. 方案名称不会改变(反正项目运行和编译正常)

回答by SLyHuy

I using Xcode 6+ and I just do:

我使用 Xcode 6+,我只是这样做:

  • Copy all files in project folders to new Folders (with new name).
  • Open *.xcodeproj or *.xcworkspace
  • Change name of Project.
  • Click on schema and delete current chema and add new one.
  • 将项目文件夹中的所有文件复制到新文件夹(使用新名称)。
  • 打开 *.xcodeproj or *.xcworkspace
  • 更改项目名称。
  • 单击架构并删除当前架构并添加新架构。

Here is done, but name of window Xcode and *.xcodeproj or *.xcworkspacestill <old-name>. Then I do:

到这里就完成了,但是窗口 Xcode 的名称*.xcodeproj or *.xcworkspace仍然是<old-name>. 然后我这样做:

  • pop install
  • Open <new name>.xcworkspace
  • pop install
  • 打开 <new name>.xcworkspace

回答by erparker

One more thing to try!

还有一件事要尝试!

When I copied all of my files, opened the project, and renamed it, everything changed to my new project name exceptfor the test target! I got a linker error that said I was missing a file called "myOldProjectname.app". Here's what fixed it:

当我复制所有文件,打开项目并重命名时,除了测试目标,所有内容都更改为我的新项目名称!我收到一个链接器错误,提示我丢失了一个名为“myOldProjectname.app”的文件。这是修复它的内容:

  1. Click on your project settings and select your test target enter image description here

  2. Click on build settings and search for "test host" enter image description here

  3. Check those 2 file paths. Chances are that those 2 paths are still pointing at your old project name. enter image description here

  1. 单击您的项目设置并选择您的测试目标 在此处输入图片说明

  2. 单击构建设置并搜索“测试主机” 在此处输入图片说明

  3. 检查这两个文件路径。有可能这 2 条路径仍然指向您的旧项​​目名称。 在此处输入图片说明

Hope that helps!

希望有帮助!

回答by Wraithseeker

For anybody else having issues with storyboard crashes after copying your project, head over to Main.storyboard under Identity Inspector.

对于在复制项目后遇到故事板崩溃问题的其他人,请转到 Identity Inspector 下的 Main.storyboard。

Next, check that your current module is the correct renamed module and not the old one.

接下来,检查您当前的模块是否是正确的重命名模块,而不是旧模块。