开发者

How to use a project as common code base

开发者 https://www.devze.com 2023-04-12 01:37 出处:网络
I have two projects which share quite a portion of logic, localizations, properties and even views (Logindialog, Popovers etc.). I now want to put the intersecting set into a new project, which should

I have two projects which share quite a portion of logic, localizations, properties and even views (Logindialog, Popovers etc.). I now want to put the intersecting set into a new project, which should be used as some sort of common library.

I want to keep the common project as a git submodule in my other projects:

Project A
- Classes
- Resources
- CommonLibrary (git submodule)

Project B
- Classes
- Resources
- SomeBSpecificFolder
- CommonLibrary (git submodule)

The CommonLibrary would now probably also be it's own Xcode project. I am not sure now how I should tackle this problem. I know of the following options:

  1. Use a library (.a-file and .h file)

    TestFlight e.g. does it like this. I quite like the fact that I can just compile against a binary and don't "see" the source. So just a clean API for doing things.

  2. Include the whole project (Project dependency)

    Three20 does it like this and I find it quite annoying to have so开发者_开发百科me 6 subprojects in my project. However in my case it would only be one project, so this might be an option too.

  3. Just have loose source files without a project inside the CommonLibrary folder and manually add them to both of the projects. The project would then compile the sources along with any project-specific sources. This looks a big like a workaround to me and I guess I would prefer to somehow bundle the common sources. Also if there are new files, I always have to add them to the other projects.

Can you guys give me some advice and tell me if there is another option or what you think are the advantages and disadvantages about these approaches?

Thanks a lot for your help!


From a clean, non-application specific perspective, you should be looking to create a library/ framework. This provides:

  • the reusability you want
  • without extending build time of dependent projects,
  • while maintaining consistency of the code base (maintainability),
  • keeping your API consistent across using projects and
  • managing dependencies between layers

In terms of Xcode, if I remember correctly from my OpenStep days, you should be looking to create a new Framework project which will provide the relevant makefiles for that type of project. A cursory surf produced:

http://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPFrameworks/Tasks/CreatingFrameworks.html

and the slightly more friendly:

http://atastypixel.com/blog/creating-applications-in-xcode-using-frameworks/

Furthermore, you might actually want to layer your frameworks to further improve reusability. That is, don't just create one framework, create one per tier: put common code in a Foundation framework, UI/ AppKit-based classes in a UI framework, etc. This way, your frameworks are relevant to whatever tier your using classes and projects live.

For instance, if you have a category on some foundation class, put it in a common/ foundation framework that can be reused by any other common/ foundation classes and projects you build. If you were to put it in a UI-tier framework with UI libraries and dependencies then it couldn't be reused by your foundation level projects (as these can't have UI-level dependencies).

Hope this helps...


I found a project called RestKit on github which seems to be a good example for a third-party framework, which can be included in an existing project.

The readme and the wiki describe nicely which steps need to be done in order to include the project. I guess this is a good starting point for someone who want to create a framework himself.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号