在 xcode/objective-c 中的项目之间共享类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2126391/
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
Sharing classes between projects in xcode/objective-c
提问by Allyn
I've got a client<=>server app I'm building for Mac OS X, using Objective-c/Cocoa and xCode. I've created a different project for both the apps I have, and I'm wondering the best way to share classes between them. There are several classes I've made that would be useful to both. This far I've been copying them around, but I feel like this isn't the best solution.
我有一个客户端<=>服务器应用程序,我正在为 Mac OS X 构建,使用 Objective-c/Cocoa 和 xCode。我为我拥有的两个应用程序创建了一个不同的项目,我想知道在它们之间共享类的最佳方式。我已经制作了几个对两者都有用的课程。到目前为止,我一直在复制它们,但我觉得这不是最好的解决方案。
How do I share classes effectively? Should I redo it as 1 project and just have two build targets? How do I do this?
如何有效地分享课程?我应该将它重做为 1 个项目并且只有两个构建目标吗?我该怎么做呢?
Any other info?
还有其他信息吗?
Thanks.
谢谢。
采纳答案by John Jacecko
If you have two or more products that are going to share a good amount of common code, like a suite of products, you might want to consider creating just a single xcode project, and then add a different target for each product that will be built from both shared and product-specific code. With a lot of shared code, a client/server pair of products would possibly be great candidates for going this way.
如果您有两个或多个要共享大量公共代码的产品,例如一套产品,您可能需要考虑只创建一个 xcode 项目,然后为每个将要构建的产品添加不同的目标来自共享代码和产品特定代码。由于有大量共享代码,客户端/服务器产品对可能是采用这种方式的绝佳候选者。
Boiled-down, the basic deal is that for each target in your xcode project that you want to build, you specify which files should be used to build it: source files, art, xibs, and so on. This way, for example, you can setup your client product to be built using files A,B,C,D,E,F, and your server product to be built using files A,F,X,Y,Z.
归根结底,基本的处理是对于你想要构建的 xcode 项目中的每个目标,你指定应该使用哪些文件来构建它:源文件、艺术、xibs 等等。这样,例如,您可以设置使用文件 A、B、C、D、E、F 构建客户端产品,使用文件 A、F、X、Y、Z 构建服务器产品。
I really like having every related product living under a single xcode project "roof", because you wont have to jump around xcode projects, and it really simplifies SCM management for the shared files.
我真的很喜欢让每个相关产品都生活在一个 xcode 项目“屋顶”下,因为您不必在 xcode 项目之间跳来跳去,而且它确实简化了对共享文件的 SCM 管理。
Here's a link to Apple's docs on this: https://developer.apple.com/library/mac/#featuredarticles/XcodeConcepts/Concept-Targets.html
这是指向 Apple 文档的链接:https: //developer.apple.com/library/mac/#featuredarticles/XcodeConcepts/Concept-Targets.html
Update: there's a little bit of extra hassle involved when it comes to configuring target-specific header files in xcode (it's always something...right?!); for example, use "myHeaderA.h" for this target and "myHeaderB.h" for that target. Here's a great post that shares how to do it: controlling which project header file Xcode will include. Caution: after you set things up this way, xcode no longer knows any paths to search for any of your target header files, so you have to set them up manually. To do this, right-click Get Info on your target, select Build category, then add your paths via the "Header Search Paths" setting. The paths are searched in the order that you enter them.
更新:在 xcode 中配置特定于目标的头文件时会涉及一些额外的麻烦(它总是......对吧?!);例如,对这个目标使用“myHeaderA.h”,对那个目标使用“myHeaderB.h”。这里有一篇很棒的帖子,分享了如何做到这一点:控制 Xcode 将包含的项目头文件. 注意:以这种方式设置后,xcode 不再知道搜索任何目标头文件的任何路径,因此您必须手动设置它们。为此,右键单击目标上的获取信息,选择构建类别,然后通过“标题搜索路径”设置添加路径。路径将按照您输入的顺序进行搜索。
回答by Chuck
A very good way to do it is to put the shared code into SCM system and include it into each project you want it in. This way every project can share the code, you can edit it anywhere and just check the changes back into source control when you're feeling good about it — and then all the other projects benefit from your work.
一个很好的方法是将共享代码放入 SCM 系统中,并将其包含到您想要的每个项目中。这样每个项目都可以共享代码,您可以在任何地方编辑它,只需将更改检查回源代码控制当你感觉良好时——然后所有其他项目都会从你的工作中受益。
This has big advantages over other ways of doing it IMO:
与 IMO 的其他方式相比,这具有很大的优势:
vs. frameworks: Packaging code into a framework is pretty annoying, and bundle-private frameworks take an unreasonably long time to load — especially just to get a few classes into your app. Wil Shipley of Omni Group (at the time) once found that the frameworks the company included in all their apps were adding several seconds to the start time of each app. Packaging private classes in a framework can also encourage more coupling than is strictly necessary — it's really tempting just to make One True Framework where all your shared classes reside, so you start assuming this code will always live together. and it becomes inseparable. Basically, frameworks are a hammer and this problem is a screw.
vs. 框架:将代码打包到框架中非常烦人,并且捆绑私有框架需要很长的时间来加载——尤其是在你的应用程序中加入一些类。Omni Group 的 Wil Shipley(当时)曾经发现该公司包含在他们所有应用程序中的框架会为每个应用程序的启动时间增加几秒钟。将私有类打包到框架中还可以鼓励更多的耦合,而不是严格必要的——将所有共享类都放在一个真正的框架中真的很诱人,因此您开始假设这些代码将始终存在在一起。并且变得密不可分。基本上,框架是一把锤子,而这个问题是一个螺丝钉。
vs. just including files: Hopefully you'll put your app into an SCM at some point anyway, and simply including the files in-place creates a problem because they'll be missing from SCM. Copying the files into each project introduces the opposite problem — each project will include its own version of the files and you'll have to manually propagate any useful changes.
与仅包含文件相比:希望您无论如何都会将您的应用程序放入 SCM,并且简单地将文件包含在原位会产生问题,因为它们会从 SCM 中丢失。将文件复制到每个项目中会引入相反的问题——每个项目都将包含自己的文件版本,您必须手动传播任何有用的更改。
回答by user151019
The best way is to create a separate framework containing the shared classes. This can be compiled up once and linked into both application projects.
最好的方法是创建一个包含共享类的单独框架。这可以编译一次并链接到两个应用程序项目中。
回答by Felix
The fastest solution is to add only references of the .h and .m files to one of the projects. Just uncheck the "copy"-checkbox in the "add existing files"-dialog in xcode. Keep in mind the you might also need to copy the referenced files if you move/share your project.
最快的解决方案是仅将 .h 和 .m 文件的引用添加到项目之一。只需取消选中 xcode 中“添加现有文件”对话框中的“复制”复选框即可。请记住,如果您移动/共享您的项目,您可能还需要复制引用的文件。
回答by james_alvarez
I had a similar issue, and the accepted answer above may cause issues for a newbie.
我有一个类似的问题,上面接受的答案可能会给新手带来问题。
It will be fine the if the two projects communicate by some protocol e.g. TCP/IP, or if they don't communicate. If however one project is a bundle (e.g. a plugin) which needs to access the same classes as an application (whilst being run in the same process) you will get issues with linking or runtime warnings/errors about having classes with the same name. The simplest way to solve this problem is to use a framework. With a framework you can set it up so all three targets are in the same project, or you can even include the framework in separate projects.
如果这两个项目通过某种协议(例如 TCP/IP)进行通信,或者它们不进行通信,那都没有问题。然而,如果一个项目是一个包(例如一个插件),它需要访问与应用程序相同的类(同时在同一进程中运行),您将遇到链接问题或运行时警告/关于具有相同名称的类的错误。解决这个问题最简单的方法是使用框架。使用框架,您可以将其设置为所有三个目标都在同一个项目中,或者您甚至可以将框架包含在单独的项目中。
I had a project with an app and a bundle plugin, here are the steps I followed in Xcode 6:
我有一个带有应用程序和捆绑插件的项目,以下是我在 Xcode 6 中遵循的步骤:
- Create a framework, copy all the shared code over.
- In the main header of the framework, include the headers of all the shared code.
- Build the framework to test it builds (e.g. select the scheme of the framework and click play)
- Go to the Build Phases section of both the Application and the Plugin Bundle and add the new framework to ‘target dependencies' and ‘Link binary with libraries'
- To include the frameworks stuff in code in the app and bundle, just use the main header, and use <> rather than “" e.g if your framework was called Foo use #import
- 创建一个框架,将所有共享代码复制过来。
- 在框架的主头文件中,包含所有共享代码的头文件。
- Build the framework to test it builds (eg 选择framework的scheme并点击play)
- 转到应用程序和插件包的构建阶段部分,并将新框架添加到“目标依赖项”和“将二进制文件与库链接”
- 要在应用程序和包的代码中包含框架内容,只需使用主标题,并使用 <> 而不是“”,例如,如果您的框架被称为 Foo,请使用 #import
Changes to the framework will be compiled automatically when you run the main app, so you can in effect ignore the fact they are different targets.
当您运行主应用程序时,将自动编译对框架的更改,因此您实际上可以忽略它们是不同目标的事实。
回答by Spina
I found a nice article on this topic here: http://www.clintharris.net/2009/iphone-app-shared-libraries/This answers my question which is specifically about multiple iPhone apps sharing code. It doesn't mention frameworks (above) so I can't say if their suggestion (xcode project references) compares favorably to the frameworks solution.
我在这里找到了一篇关于这个主题的好文章:http: //www.clintharris.net/2009/iphone-app-shared-libraries/这回答了我的问题,特别是关于多个 iPhone 应用程序共享代码的问题。它没有提到框架(上面)所以我不能说他们的建议(xcode 项目参考)是否与框架解决方案相比更有利。