Xcode 和iPhone - 在多个项目中重用代码的最佳方式?
在项目中重用代码的最佳方法是什么?
假设我实现了一个 UI 元素,并且希望它可以在我的 iphone 和 ipad 应用程序中使用,而不必复制代码并拥有 2 个副本。
What is the best way to reuse code within projects?
Let's say I implemented a UI Element and I want it to be used in both my iphone and ipad application without having to copy the code over and have 2 copies of it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需创建一个项目,其中包含 XCode 中的所有共享代码,并在 iPhone 和 iPad 应用程序项目中引用该项目。简单明了。
Just create a project, which includes all your shared code in XCode and reference this project in your iPhone and iPad application project. Plain and simple.
对我来说,我会创建一个静态库项目,其中包含 Xcode 中的共享代码(示例中的 UI 元素)。
然后,当我需要在 iPhone 或 iPad 应用程序项目中使用该库时,我可以通过将项目拖放到项目导航器来引用静态库项目,并配置正确的依赖项、使用的库和标题搜索路径。通过这种方式,您始终拥有单一的库源代码,以便于维护和修改。
当然,你实际上可以将静态库编译成二进制并将其链接到你的项目,但是当你发现静态库中存在错误并且需要切换到另一个项目来纠正错误,然后进行编译和复制时,它就不太灵活了的二进制库文件。
我刚刚写了一篇关于如何将静态库项目链接到 Xcode 4.3.2 上的 iOS 项目的文章(链接如下)。这可能对您解决遇到的标头未找到问题有用。希望这有帮助。
http://vicidi.wordpress.com/2012/04/11/linking-an-ios-static-library-project-in-another-project-in-xcode-4-3-2/
For me I would make a static library project which contains the shared code (UI Element in your example) in Xcode.
Then when I need to use the library in the iPhone or iPad app project, then I can just reference the static library project by drag and drop the project to the Project Navigator and configure the correct dependency, library used and header search path. In this way you always have a single source of library source code for easier maintenance and modification.
Certainly you can actually compile the static library into binary and link it to your project, but it just not too flexible when you find bugs in your static library and need to switch to another project to correct the bug, and then do the compile and copy of the binary library file.
I have just wrote an article (link below) on how to link up the static library project to an iOS project on Xcode 4.3.2. This maybe useful for you to solve the header not found problem you encountered. Hope this help.
http://vicidi.wordpress.com/2012/04/11/linking-an-ios-static-library-project-in-another-project-in-xcode-4-3-2/