简体   繁体   中英

What's an easy way to manage source files that are shared among different Xcode projects?

I'm diving into iOS development and I plan to build a few basic apps that make use of my own reusable code. As I update the files that contain my reusable code, I'd like those updates to be reflected in all the Xcode projects that use them. What are some ways I can do this?

Thanks in advance for your help!

There are a few ways you can do it.

If your "reusable code" is separated into libraries, you could package it up as individual projects (that compile to static libraries) and then link them into your main project (that compiles into an executable). A bit tricky, but it's the "proper" way. (Not as flexible or easy as Visual Studio's solution/project paradigm, but that's a rant for another day.)

You could also just drag-and-drop a directory containing the source files into the Project window directly, either using "Create Folder References" or "Recursively create groups". (Either way, you don't want to use the "Copy items" checkbox at the top if you want only one copy of the source files.)

I've done all three.

You may want to start with the latter -- group your "reusable code" into a folder somewhere (so there's only one copy) and then drag-and-drop that folder into your project using "Recursively create groups". One warning: if you do that in multiple projects, and sometime later you want to remove it from the project, don't use the option to send it to the trash, or your other projects could get messed up.

这听起来像是你可以使用Git和分支的东西。

创建一个静态库项目,然后使用Xcode的项目间依赖项功能以正确的顺序构建它们并将应用程序与静态库链接

One solution I've thought of is to build a static library project, but only use it just to test compile the reusable code in that library. Then symlink the reusable source code subdirectory of that library project into all other projects that use that code. That way all the source code is available in any project, and any changes I make in the reusable code will be reflected everywhere else immediately (no copy step). And of course, use a good source code revision control methodology on that subdirectory, so that I can version it, compare changes, revert, etc.

the proper way is to create a library. in iOS, you're restricted to static libraries, but dynamic libraries and frameworks are other common possibilities (for example, if you're targeting OS X).

to configure this (using libStatic as a name for the static library, and App as the app in this example):

1) create the libStatic project, and configure it to compile the files you choose

2) add libStatic.xcodeproj to the App.xcodeproj. this adds the project symbols, indexes and references to the App project.

(the remaining steps all take place in App.xcodeproj)

3) double click the target which depends on liStatic

4) navigate to General tab

5) click '+' under the dependencies list, and add the libStatic target to the dependencies. it is important that you create this association/dependency so the libStatic target will be kept up to date as you develop in App (it will be built before App).

6) navigate to the libStatic project reference in the groups & files tree. if necessary, click (expand) the disclosure triangle to view the products of libStatic.

7) drag the appropriate product from the libStatic project reference into the App target's link stage.

8) clean, build, run

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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