简体   繁体   English

从 Xcode 项目导出文件

[英]Export files from Xcode project

I currently have an Xcode workspace with an app project and the pod project (as commonly happens).我目前有一个 Xcode 工作区,其中包含一个应用程序项目和 pod 项目(通常会发生)。 I would like to "export" some of the files containing classes and structs that would be preparatory for a new project that should begin soon.我想“导出”一些包含类和结构的文件,这些文件将为即将开始的新项目做准备。 These files are totally separate from each other and should not be exported as a library or framework because that should require to keep everything updated.这些文件彼此完全分开,不应作为库或框架导出,因为这需要保持所有内容更新。 Instead, I would rather bring these files out of the app project, but keep them into the workspace.相反,我宁愿将这些文件带出应用程序项目,而是将它们保留在工作区中。 Is it possibile?有可能吗? If so, how can I achieve this goal?如果是这样,我怎样才能实现这个目标?

When you want to keep your common code and be able to maintain it as one code on both projects then I suggest you still use a framework.当你想保留你的公共代码并能够在两个项目中将其作为一个代码进行维护时,我建议你仍然使用框架。 It is hard to tell from your question what you mean by "because that should require to keep everything updated" but using a framework within your workspace works awesomely in Xcode and any change you do within framework is instantly reflected in your code.很难从您的问题中看出“因为这应该需要保持所有内容更新”的意思,但是在您的工作区中使用框架在 Xcode 中非常有效,并且您在框架内所做的任何更改都会立即反映在您的代码中。 So no extra deployment work is needed at all.所以根本不需要额外的部署工作。 To do so you simply do the following:为此,您只需执行以下操作:

  1. Create a new project selecting Framework创建一个新项目选择框架
  2. When creating the project select your workspace from dropdown menu (it is at step where you select project folder location)创建项目时,从下拉菜单中选择您的工作区(在您选择项目文件夹位置的步骤中)
  3. In Xcode file navigator in newly created framework project open "Products" directory which should contain (in red) FrameworkName.framework在新创建的框架项目的 Xcode 文件导航器中打开“产品”目录,其中应包含(红色) FrameworkName.framework
  4. Open your main project target settings and open General , scroll down to Frameworks, Libraries... and drag that .framework file in this list打开您的主要项目目标设置并打开General ,向下滚动到Frameworks, Libraries...并将该 .framework 文件拖动到此列表中

That is all.就这些。 Now you can use your framework in your code.现在您可以在代码中使用您的框架。 Simply import it in any Swift file you want to.只需将其导入到您想要的任何 Swift 文件中即可。

If at this point you are worried that you need to add import MyFramework into each and every Swift file where you use it then there is a shortcut.如果此时您担心需要将import MyFramework添加到您使用它的每个 Swift 文件中,那么有一个快捷方式。 You can simply add a new Swift file and use type aliases to avoid this problem:你可以简单地添加一个新的 Swift 文件并使用类型别名来避免这个问题:

import MyFramework

typealias Class1 = MyFramework.Class1
typealias Class2 = MyFramework.Class2
typealias Class3 = MyFramework.Class3

Not the cleanest solution but it does the job.不是最干净的解决方案,但它可以完成工作。

On the other hand if you wish to extract files and use them in a new project but not share the code.另一方面,如果您希望提取文件并在新项目中使用它们但不共享代码。 So then each of the projects may eventually end up with different implementations of these files then you need to duplicate the files.那么每个项目最终可能会以这些文件的不同实现结束,然后您需要复制这些文件。 You can do that easiest with file system by copying source files themselves.您可以通过复制源文件本身来最简单地使用文件系统。

You can then put them into workspace under whatever you want.然后,您可以将它们放入任何您想要的工作区。 If you wish that currently none of the projects contain these files you may simply select files in Xcode and uncheck Target Membership for all projects on file settings.如果您希望当前没有任何项目包含这些文件,您可以简单地在 Xcode 中选择文件,并在文件设置中取消选中所有项目的目标成员资格

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

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