如何在 Xcode 中创建一组可重用的代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/159221/
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
How do I create a bundle of reusable code in Xcode?
提问by
I am developing an iPhone app and have to parse xml files in order to put them into a database. I will also be using those same xml parsers in my app so users can import their own data. I was wondering how I can extract those xml parsers into a bundle or a library so I can use them both in my iPhone app and in a command line app where I just populate a sqlite3 database.
我正在开发一个 iPhone 应用程序,必须解析 xml 文件才能将它们放入数据库中。我还将在我的应用程序中使用这些相同的 xml 解析器,以便用户可以导入他们自己的数据。我想知道如何将这些 xml 解析器提取到一个包或一个库中,以便我可以在我的 iPhone 应用程序和命令行应用程序中使用它们,我只是在其中填充 sqlite3 数据库。
Thanks in advance!
提前致谢!
回答by millenomi
Create a static library project, then use the interproject dependency feature of Xcode to build them in the correct order and link the app with the static library. You'll need to have a common build directory set for all the projects for this to work correctly (at least you did around Xcode 3.0, didn't check if this is still a problem with 3.1).
创建一个静态库项目,然后使用 Xcode 的项目间依赖功能以正确的顺序构建它们并将应用程序与静态库链接。您需要为所有项目设置一个通用构建目录才能使其正常工作(至少您在 Xcode 3.0 附近做了,没有检查这是否仍然是 3.1 的问题)。
You can set the build directory from the target or project's build settings (in the Get Info pane). To create an interpoject dependency:
您可以从目标或项目的构建设置(在“获取信息”窗格中)设置构建目录。要创建项目间依赖项:
- Drag the library project into the application project's Files & Groups pane.
- Set up target dependency in the application target's Get Info pane. Make it dependent on the library's target.
- Drag the library product in the application target's Link With Libraries step. You can find the library product by expanding the library project within the app project's Files & Groups (click the arrow).
- 将库项目拖到应用程序项目的文件和组窗格中。
- 在应用程序目标的“获取信息”窗格中设置目标依赖项。使其依赖于库的目标。
- 在应用程序目标的链接库步骤中拖动库产品。您可以通过在应用程序项目的文件和组中展开库项目来找到库产品(单击箭头)。
Sounds more complicated than it is. It isn't much.
听起来比它更复杂。这并不多。
(Small extras: yes, you need a common build folder as indicated in the Xcode Project Management Guide, and the Xcode Build System Guidecan help you "get" Xcode's build system, which -- at the cost of starting a religion war -- I think is one of the most flexible and simple build systems out there.)
(额外的小东西:是的,您需要一个通用的构建文件夹,如Xcode 项目管理指南 中所示,Xcode 构建系统指南可以帮助您“获得”Xcode 的构建系统,这——以发动一场宗教War为代价——我认为这是目前最灵活、最简单的构建系统之一。)