xcode 如何为 Cocoa Pods 正确配置我的项目?

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

How do I configure my project for Cocoa Pods correctly?

xcodecocoapods

提问by Senseful

In some of my past projects, Cocoa Pods wasn't configured correctly and it would lead to undesirable things such as needing duplicate Cocoa Pod projects in the Project Navigator, or pod files having to be checked in to version control.

在我过去的一些项目中,Cocoa Pods 没有正确配置,这会导致不受欢迎的事情,例如需要在项目导航器中复制 Cocoa Pod 项目,或者必须将 pod 文件签入到版本控制中。

How do I install Cocoa Pods correctly and verify that my Directory structure and Project Navigator structures are set up properly?

如何正确安装 Cocoa Pods 并验证我的目录结构和项目导航器结构是否正确设置?

回答by Senseful

The best way to use Cocoa Pods is to follow the official example'slead.

使用 Cocoa Pods 的最佳方法是遵循官方示例的指导。

This consists of several steps.

这包括几个步骤。

Setting up Cocoa Pods for the first time

首次设置 Cocoa Pods

1. Install Cocoa Pods on your computer

1. 在你的电脑上安装 Cocoa Pods

Begin with the Getting Started Guideto install Cocoa Pods on your computer.

入门指南开始在您的计算机上安装 Cocoa Pods。

2. Create a Podfile

2.创建一个Podfile

Do not create the Podfile in Xcode, since it should notbe part of your project!

不要在 Xcode 中创建 Podfile,因为它应该是你项目的一部分!

Create the file in your favorite text editor, and save it at the same level as your .xcodeprojfile.

在您喜欢的文本编辑器中创建文件,并将其保存在与.xcodeproj文件相同的级别。

An example of a Podfile's contents are:

Podfile 内容的一个例子是:

pod 'CupertinoYankee', '~> 1.0'

3. Run pod install

3. 运行 pod install

Navigate to your Podfile's directory in terminal and run pod install.

在终端中导航到 Podfile 的目录并运行pod install.

4. Open the generated workspace

4. 打开生成的工作区

Once you run pod install, you will see a message such as:

运行后pod install,您将看到一条消息,例如:

[!] From now on use MyProject.xcworkspace.

[!] 从现在开始使用MyProject.xcworkspace

Close the Xcode project, and open up the generated .xcworkspacefile.

关闭 Xcode 项目,然后打开生成的.xcworkspace文件。

Understanding the Changes

了解变化

It is always useful to know what changes were made to your project in case things go wrong.

如果出现问题,了解对您的项目进行了哪些更改总是很有用的。

1. Directory structure

1. 目录结构

You should have started with a structure such as:

您应该从一个结构开始,例如:

enter image description here

在此处输入图片说明

... then manually added a Podfile:

...然后手动添加一个Podfile:

enter image description here

在此处输入图片说明

After running pod install, your directory structure now looks like this:

运行 pod install 后,您的目录结构现在如下所示:

enter image description here

在此处输入图片说明

Notice how the only new objects are:

注意唯一的新对象是如何:

  • MyProject.xcworkspace
  • Podfile.lock
  • Pods
  • 我的项目.xcworkspace
  • Podfile.lock
  • 豆荚

2. Project Navigator

2. 项目导航

In Xcode, you started with this:

在 Xcode 中,你是这样开始的:

enter image description here

在此处输入图片说明

... and ended with this in the new workspace:

...并在新工作区中以这个结束:

enter image description here

在此处输入图片说明

For reference, here is an expanded look at the new directory structure:

作为参考,以下是新目录结构的扩展视图:

enter image description here

在此处输入图片说明

Obviously, the Pods project was added to the Project Navigator, but there are a few other key things to notice:

显然,Pods 项目已添加到 Project Navigator 中,但还有一些其他关键事项需要注意:

Notice how Podfileis located at the root of the MyProject folder in Finder, but is inside of the Pods project in Xcode's project navigator.

请注意 howPodfile位于 Finder 中 MyProject 文件夹的根目录,但位于 Xcode 项目导航器中的 Pods 项目内。

Likewise, notice how Pods.xcconfigis located inside the Pods folder in Finder, but is inside of the MyProject in Xcode's project navigator.

同样,注意 howPods.xcconfig位于 Finder 中的 Pods 文件夹内,但位于 Xcode 项目导航器中的 MyProject 内。

3. Configurations

3. 配置

These are the same screenshots as before:

这些是与之前相同的屏幕截图:

In Xcode, you started with this:

在 Xcode 中,你是这样开始的:

enter image description here

在此处输入图片说明

... and ended with this in the new workspace:

...并在新工作区中以这个结束:

enter image description here

在此处输入图片说明

Notice how under Configurations, the "MyProject" target now has Podsspecified.

请注意在配置下,“MyProject”目标现在已Pods指定。

This is important because otherwise PODS_ROOT will not be set up properly (among other things), and when you build, you will get errors.

这很重要,否则 PODS_ROOT 将无法正确设置(除其他外),并且在构建时,您将收到错误消息

4. PODS_ROOT added

4. 添加 PODS_ROOT

Your target's Build Settingswent from this: (Note: this is scrolled all the way down)

您的目标的构建设置来自:(注意:这是一直向下滚动的)

enter image description here

在此处输入图片说明

... to this:

...到这个:

enter image description here

在此处输入图片说明

Notice how PODS_ROOTwas added as a User-Defined setting.

请注意如何PODS_ROOT添加为用户定义的设置。

Note: this part of the configuration sometimes gets bugged out when reinstalling Pods, so you may need to close and open the Xcode projectto fix it.

注意:这部分配置在重新安装 Pod 时有时会出现错误,因此您可能需要关闭并打开 Xcode 项目来修复它。

5. Other changes

5. 其他变化

You can see a complete list of the changes here.

您可以在此处查看完整的更改列表



回答by Md. Sulayman

In my case:On my project SwiftLintwas added on Build Phasees -> SwiftLintas below image SwiftLintMy root Folder structure was Volume/Project Files/iOS Project Files/Office Projects/project-name. As you can see there were space on my Directory structure , SwiftLint executed on build phase create problem for those space and show a no directory founderror. After changing the high level directory by renaming like the below image solved my problem Project directory after rename

就我而言:在我的项目SwiftLint中添加Build Phasees -> SwiftLint如下图SwiftLint我的根文件夹结构是Volume/Project Files/iOS Project Files/Office Projects/project-name. 如您所见,我的目录结构中有空间,在构建阶段执行的 SwiftLint 为这些空间创建问题并显示no directory found错误。通过重命名更改高级目录后,如下图解决了我的问题重命名后的项目目录