如何完全重命名 Xcode 项目(即包括文件夹)?

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

How do I completely rename an Xcode project (i.e. inclusive of folders)?

xcodeiderenamebatch-rename

提问by Vatsal Manot

I have a project named XXX. I want to rename this project to YYY.

我有一个名为XXX. 我想将此项目重命名为YYY.

Note that XXXis an extremely common term (for e.g. "data", or simply "project"), and thus a simple RegEx search-and-replace is not possible, out of risk of corrupting the project configuration files.

请注意,这XXX是一个非常常见的术语(例如“数据”,或简称为“项目”),因此,出于破坏项目配置文件的风险,不可能进行简单的 RegEx 搜索和替换。

My current project directory contains the following items:

我当前的项目目录包含以下项目:

  • XXX
  • XXXTests
  • XXX.xcodeproj
  • XXX
  • XXXTests
  • XXX.xcodeproj

and I want to rename them to:

我想将它们重命名为:

  • YYY
  • YYYTests
  • YYY.xcodeproj
  • YYY
  • YYYTests
  • YYY.xcodeproj

... respectively, with the necessary changes being reflected in my project file.

...分别,必要的更改反映在我的项目文件中。

How can I accomplish this without having to manually create and populate a new project?

如何在无需手动创建和填充新项目的情况下完成此操作?



Edit:It is absolutely ridiculous that this has become my most upvoted question.

编辑:这已成为我投票最多的问题,这绝对是荒谬的。

回答by Luke West

Step 1 - Rename the project

步骤 1 - 重命名项目

  1. Click on the project you want to rename in the "Project navigator" in the left panel of the Xcode window.
  2. In the right panel, select the "File inspector", and the name of your project should be found under "Identity and Type". Change it to your new name.
  3. When the dialog asks whether to rename or not rename the project's content items, click "Rename".
  1. 在 Xcode 窗口左侧面板的“项目导航器”中单击要重命名的项目。
  2. 在右侧面板中,选择“文件检查器”,您的项目名称应位于“身份和类型”下。将其更改为您的新名称。
  3. 当对话框询问是否重命名项目的内容项时,单击“重命名”。

Step 2 - Rename the scheme

第 2 步 - 重命名方案

  1. At the top of the window, next to the "Stop" button, there is a scheme for your product under its old name; click on it, then choose "Manage Schemes…".
  2. Click on the old name in the scheme and it will become editable; change the name and click "Close".
  1. 在窗口顶部,“停止”按钮旁边,有一个旧名称下的产品方案;单击它,然后选择“管理方案...”。
  2. 单击方案中的旧名称,它将变得可编辑;更改名称并单击“关闭”。

Step 3 - Rename the folder with your assets

第 3 步 - 使用您的资产重命名文件夹

  1. Quit Xcode.
  2. In the correctly-named master folder, there is a newly-named .xcodeproj file with the wrongly-named OLD folder. Rename the OLD folder to your new name (if you use Git, you could run git mv oldname newnameso that Git recognizes this is a move, rather than deleting/adding new files).
  3. Re-open the project in Xcode and you will see a warning "The folder OLD does not exist"; dismiss the warning.
  4. In the "Project navigator" in the left-hand panel, click on the top-level OLD folder.
  5. In the right-hand panel, under "Identity and Type", change the "Name" field from the OLD name to the new name.
  6. Just below that field is a "Location" menu. Click on the nearby folder icon and choose the renamed folder.
  1. 退出 Xcode。
  2. 在正确命名的主文件夹中,有一个新命名的 .xcodeproj 文件和错误命名的 OLD 文件夹。将 OLD 文件夹重命名为您的新名称(如果您使用 Git,您可以运行git mv oldname newname以便 Git 识别出这是一个移动,而不是删除/添加新文件)。
  3. 在Xcode中重新打开项目,你会看到一个警告“文件夹OLD不存在”;解除警告。
  4. 在左侧面板的“项目导航器”中,单击顶级 OLD 文件夹。
  5. 在右侧面板的“身份和类型”下,将“名称”字段从旧名称更改为新名称。
  6. 该字段正下方是“位置”菜单。单击附近的文件夹图标并选择重命名的文件夹。

Step 4 - Rename the Build plist data

第 4 步 - 重命名 Build plist 数据

  1. Click on the project in the "Project navigator" on the left, and in the main panel select "Build Settings".
  2. Search for "plist" in the settings.
  3. In the Packaging section, you will see Info.plistand Product Bundle Identifier.
  4. If there is a name entered in Info.plist, update it.
  5. Do the same for Product Bundle Identifier.
  6. If you use SwiftUI, search for "Development Assets" and update the path.
  1. 单击左侧“项目导航器”中的项目,然后在主面板中选择“构建设置”。
  2. 在设置中搜索“plist”。
  3. 在“包装”部分,您将看到Info.plistProduct Bundle Identifier
  4. 如果在 中输入了名称Info.plist,请更新它。
  5. Product Bundle Identifier.
  6. 如果您使用 SwiftUI,请搜索“Development Assets”并更新路径。

Step 5 - Repeat step 3 for tests (if you have them)

第 5 步 - 重复第 3 步进行测试(如果有)

Step 6 - Repeat step 3 for core data if its name matches project name (if you have it)

步骤 6 - 如果核心数据的名称与项目名称匹配(如果有),则对核心数据重复步骤 3

Step 7 - Rebuild your project

第 7 步 - 重建您的项目

  1. Command+ Shift+ Kto clean
  2. Command+ Bto build
  1. Command+ Shift+K清洁
  2. Command+B建造

回答by Vaiden

To add to @luke-west 's excellent answer:

添加到@luke-west 的优秀答案中:

When using CocoaPods

使用 CocoaPods 时

After step 2:

在第 2 步之后:

  1. Quit XCode.
  2. In the master folder, rename OLD.xcworkspaceto NEW.xcworkspace.
  1. 退出 XCode。
  2. 在主文件夹中,重命名OLD.xcworkspaceNEW.xcworkspace.

After step 4:

第 4 步后:

  1. In XCode: choose and edit Podfilefrom the project navigator. You should see a targetclause with the OLD name. Change it to NEW.
  2. Quit XCode.
  3. In the project folder, delete the OLD.podspecfile.
  4. rm -rf Pods/
  5. Run pod install.
  6. Open XCode.
  7. Click on your project name in the project navigator.
  8. In the main pane, switch to the Build Phasestab.
  9. Under Link Binary With Libraries, look for libPods-OLD.aand delete it.
  10. If you have an objective-c Bridging header go to Build settings and change the location of the header from OLD/OLD-Bridging-Header.h to NEW/NEW-Bridging-Header.h
  11. Clean and run.
  1. 在 XCode 中:Podfile从项目导航器中选择和编辑。您应该会看到一个target带有 OLD 名称的子句。将其更改为 NEW。
  2. 退出 XCode。
  3. 在项目文件夹中,删除该OLD.podspec文件。
  4. rm -rf Pods/
  5. 运行pod install
  6. 打开 XCode。
  7. 在项目导航器中单击您的项目名称。
  8. 在主窗格中,切换到Build Phases选项卡。
  9. 在 下Link Binary With Libraries,查找libPods-OLD.a并删除它。
  10. 如果您有一个objective-c Bridging 头文件,请转到构建设置并将头文件的位置从 OLD/OLD-Bridging-Header.h 更改为 NEW/NEW-Bridging-Header.h
  11. 清洁并运行。

回答by Pietro Basso

A quicker solution using shell commands (works with CocoaPods too):

使用 shell 命令的更快解决方案(也适用于 CocoaPods):

PLEASE cdTO A NON-GIT REPOSITORY BEFORE PROCEEDING ??

cd在继续之前使用非 Git 存储库?

Step 1 - Prerequisites

步骤 1 - 先决条件

  1. Copy your original project folder to a temporary /NewProjectFolderOUTSIDE your git repository. ?? changes to .git could corrupt your git index?
  1. 将您的原始项目文件夹复制到/NewProjectFoldergit 存储库之外的临时文件夹?? 对 .git 的更改可能会破坏您的 git 索引

Step 2 - Open Terminal

第 2 步 - 打开终端

Now we're going to rename the project from oldNameto NewProject.

现在我们要将项目从 重命名oldNameNewProject.

  • Close XCode.
  • Go to your /NewProjectFolder.
  • 关闭 XCode。
  • 转到您的/NewProjectFolder.
cd /Path/to/your/NewProjectFolder
  • Install the extra tools needed.
  • 安装所需的额外工具。
brew install rename ack
  • Rename the files and directories containing the source string. You'll need to RUN THIS COMMAND TWICE, because directories will be renamed first, then files and directories inside those will be renamed on the next iteration.
  • 重命名包含源字符串的文件和目录。您需要两次运行此命令,因为目录将首先重命名,然后在下一次迭代中将重命名其中的文件和目录。
find . -name 'oldName*' -print0 | xargs -0 rename --subst-all 'oldName' 'NewProject'
  • Check if all the files containing the source string are renamed. You should see empty output.
  • 检查是否所有包含源字符串的文件都被重命名。您应该看到空输出。
find . -name 'oldName*'
  • Replace all occurrences of the string in all files.
  • 替换所有文件中出现的所有字符串。
ack --literal --files-with-matches 'oldName' --print0 | xargs -0 sed -i '' 's/oldName/NewProject/g'
  • Check if all occurrences of the string in all files were replaced. You should see empty output.
  • 检查是否替换了所有文件中所有出现的字符串。您应该看到空输出。
ack --literal 'oldName'
  • Run pod install
  • Add NewProjectFolderto your repository.
  • You are done!
  • pod install
  • 添加NewProjectFolder到您的存储库。
  • 你完成了!

回答by Olcay Erta?

There is a GitHub project called Xcode Project Renamer:

有一个 GitHub 项目叫做Xcode Project Renamer

It should be executed from inside root of Xcode project directory and called with two string parameters: $OLD_PROJECT_NAME & $NEW_PROJECT_NAME

Script goes through all the files and directories recursively, including Xcode project or workspace file and replaces all occurrences of $OLD_PROJECT_NAME string with $NEW_PROJECT_NAME string (both in each file's name and content).

它应该从 Xcode 项目目录的根目录中执行,并使用两个字符串参数调用: $OLD_PROJECT_NAME & $NEW_PROJECT_NAME

脚本递归遍历所有文件和目录,包括 Xcode 项目或工作区文件,并用 $NEW_PROJECT_NAME 字符串(在每个文件的名称和内容中)替换所有出现的 $OLD_PROJECT_NAME 字符串。

DON'T FORGET TO BACKUP YOUR PROJECT!

不要忘记备份您的项目!

Xcode Project Renamer

Xcode 项目重命名器

回答by Kiran Jasvanee

XCode 11.0+.

XCode 11.0+。

It's really simple now. Just go to Project Navigatorleft panel of the XCode window.
Press Enterto make it active for rename, just like you change the folder name.
enter image description here

现在真的很简单。只需转到Project NavigatorXCode 窗口的左侧面板即可。
按下Enter以激活重命名,就像更改文件夹名称一样。
在此处输入图片说明

Just change the new name here, and XCode will ask you for renaming other pieces of stuff.

只需在此处更改新名称,XCode 就会要求您重命名其他内容。

enter image description here.

在此处输入图片说明.

Tap on Renamehere and you are done.
If you are confused about your root folder name that why it's not changed, well it's just a folder. just renamed it with a new name.
enter image description here

点击Rename这里,你就完成了。
如果你对你的根文件夹名称感到困惑,为什么它没有改变,那么它只是一个文件夹。只是用一个新名字重命名了它。
在此处输入图片说明

回答by Paal Aune

To change the project name;

更改项目名称;

  1. Select your project in the Project navigator.
  1. 在项目导航器中选择您的项目。

2.In the Identity and Type section of the File inspector, enter a new name into the Name field.

2. 在文件检查器的身份和类型部分,在名称字段中输入一个新名称。

3.Press Return.

3.按回车。

A dialog is displayed, listing the items in your project that can be renamed. The dialog includes a preview of how the items will appear after the change.

将显示一个对话框,列出项目中可以重命名的项目。该对话框包括更改后项目将如何显示的预览。

  1. To selectively rename items, disable the checkboxes for any items you don't want to rename. To rename only your app, leave the app selected and deselect all other items.

  2. Press "Rename"

  1. 要有选择地重命名项目,请禁用您不想重命名的任何项目的复选框。要仅重命名您的应用程序,请选择该应用程序并取消选择所有其他项目。

  2. 按“重命名”

回答by itsmcgh

Extra instructions when following @Luke-West's + @Vaiden's solutions:

遵循@Luke-West + @Vaiden 的解决方案时的额外说明:

  • If your scheme has not changed (still showing my mac) on the top left next to the stop button:

    1. Click NEWLY created Project name (next to stop button) > Click Edit Schemes > Build (left hand side) > Remove the old target (will say it's missing) and replace with the NEWLY named project under NEWLY named project logo
  • 如果您的方案在停止按钮旁边的左上角没有改变(仍然显示我的 mac):

    1. 单击新创建的项目名称(停止按钮旁边)> 单击编辑方案> 构建(左侧)> 删除旧目标(会说它丢失)并替换为新命名的项目下的新命名的项目徽标

Also, I did not have to use step 3 of @Vaiden's solution. Just running rm -rf Pods/in terminal got rid of all old pod files

另外,我不必使用@Vaiden 解决方案的第 3 步。只是rm -rf Pods/在终端中运行摆脱了所有旧的 pod 文件

I also did not have to use step 9 in @Vaiden's solution, instead I just removed the OLD project named framework under Link Binary Libraries (the NEWLY named framework was already there)

我也不必在@Vaiden 的解决方案中使用第 9 步,相反,我只是删除了 Link Binary Libraries 下名为 framework 的 OLD 项目(新命名的框架已经存在)

So the updated steps would be as follows:

所以更新的步骤如下:

Step 1 - Rename the project

步骤 1 - 重命名项目

  1. If you are using cocoapods in your project, close the workspace, and open the XCode project for these steps.
  2. Click on the project you want to rename in the "Project navigator" on the left of the Xcode view.
  3. On the right select the "File inspector" and the name of your project should be in there under "Identity and Type", change it to the new name.
  4. Click "Rename" in a dropdown menu
  1. 如果您在项目中使用 cocoapods,请关闭工作区,然后打开 XCode 项目以执行这些步骤。
  2. 在 Xcode 视图左侧的“项目导航器”中单击要重命名的项目。
  3. 在右侧选择“文件检查器”,您的项目名称应该在“身份和类型”下,将其更改为新名称。
  4. 在下拉菜单中单击“重命名”

Step 2 - Rename the Scheme

第 2 步 - 重命名方案

  1. In the top bar (near "Stop" button), there is a scheme for your OLD product, click on it, then go to "Manage schemes"
  2. Click on the OLD name in the scheme, and it will become editable, change the name
  3. Quit XCode.
  4. In the master folder, rename OLD.xcworkspace to NEW.xcworkspace.
  1. 在顶部栏中(“停止”按钮附近),有一个适用于您的旧产品的方案,单击它,然后转到“管理方案”
  2. 点击方案中的OLD名称,它会变成可编辑的,更改名称
  3. 退出 XCode。
  4. 在主文件夹中,将 OLD.xcworkspace 重命名为 NEW.xcworkspace。

Step 3 - Rename the folder with your assets

第 3 步 - 使用您的资产重命名文件夹

  1. Quit Xcode
  2. In the correctly named master folder, there is a newly named xcodeproj file with the the wrongly named OLD folder. Rename the OLD folder to your new name
  3. Reopen the project, you will see a warning: "The folder OLD does not exist", dismiss the warning
  4. In the "Project navigator" on the left, click the top level OLD folder name
  5. In Utilities pane under "Identity and type" you will see the "Name" entry, change this from the OLD to the new name
  6. Just below there is a "Location" entry. Click on a folder with the OLD name and chose the newly renamed folder
  1. 退出 Xcode
  2. 在正确命名的主文件夹中,有一个新命名的 xcodeproj 文件,其中包含错误命名的 OLD 文件夹。将 OLD 文件夹重命名为您的新名称
  3. 重新打开项目,会看到警告:“文件夹OLD不存在”,解除警告
  4. 在左侧的“项目导航器”中,单击顶级 OLD 文件夹名称
  5. 在“身份和类型”下的实用程序窗格中,您将看到“名称”条目,将其从旧名称更改为新名称
  6. 正下方有一个“位置”条目。单击具有 OLD 名称的文件夹并选择新重命名的文件夹

Step 4 - Rename the Build plist data

第 4 步 - 重命名 Build plist 数据

  1. Click on the project in the "Project navigator" on the left, in the main panel select "Build Settings"
  2. Search for "plist" in this section Under packaging, you will see Info.plist, and Product bundle identifier
  3. Rename the top entry in Info.plist
  4. Do the same for Product Identifier
  1. 点击左侧“项目导航器”中的项目,在主面板中选择“构建设置”
  2. 在本节中搜索“plist” 在打包下,您将看到 Info.plist 和产品包标识符
  3. 重命名 Info.plist 中的顶部条目
  4. 对产品标识符执行相同操作

Step 5 Handling Podfile

步骤 5 处理 Podfile

  1. In XCode: choose and edit Podfile from the project navigator. You should see a target clause with the OLD name. Change it to NEW.
  2. Quit XCode.
  3. In terminal, cd into project directory, then: rm -rf Pods/
  4. Run pod install.
  5. Open XCode.
  6. Click on your project name in the project navigator.
  7. In the main pane, switch to the Build Phases tab. Under Link Binary With Libraries, look for the OLD framework and remove it (should say it is missing) The NEWLY named framework should already be there, if not use the "+" button at the bottom of the window to add it
  8. If you have an objective-c Bridging header go to Build settings and change the location of the header from OLD/OLD-Bridging-Header.h to NEW/NEW-Bridging-Header.h
  9. Clean and run.
  1. 在 XCode 中:从项目导航器中选择并编辑 Podfile。您应该会看到一个带有 OLD 名称的目标子句。将其更改为 NEW。
  2. 退出 XCode。
  3. 在终端中,cd 进入项目目录,然后: rm -rf Pods/
  4. 运行 pod 安装。
  5. 打开 XCode。
  6. 在项目导航器中单击您的项目名称。
  7. 在主窗格中,切换到 Build Phases 选项卡。在 Link Binary With Libraries 下,查找 OLD 框架并将其删除(应该说它丢失了) NEWLY 命名的框架应该已经存在,如果没有,请使用窗口底部的“+”按钮添加它
  8. 如果您有一个objective-c Bridging 头文件,请转到构建设置并将头文件的位置从 OLD/OLD-Bridging-Header.h 更改为 NEW/NEW-Bridging-Header.h
  9. 清洁并运行。

You should be able to build with no errors after you have followed all of the steps successfully

成功执行所有步骤后,您应该能够在没有错误的情况下进行构建

回答by CristianMoisei

Aside from all the steps Luke and Vaiden recommended, I also had to rename all the customModule properties in my Storyboard to match the new name, and this has to be case sensitive.

除了 Luke 和 Vaiden 推荐的所有步骤之外,我还必须重命名我的 Storyboard 中的所有 customModule 属性以匹配新名称,这必须区分大小写。