简体   繁体   English

在Xcode中共享项目之间的代码

[英]Share code between projects in Xcode

I made a generic app, and from that app, I use different images to make different apps (same app, different images). 我制作了一个通用应用程序,从该应用程序,我使用不同的图像来制作不同的应用程序(相同的应用程序,不同的图像)。

My idea until now has been every time I was generating a new app, I copied the code and changed the images (and sometimes some lines of code). 直到现在我的想法一直是我每次生成一个新的应用程序,我复制了代码并更改了图像(有时还有一些代码行)。 But every time I change something in the main app, I have to change it in all the sub-apps. 但每次我在主应用程序中更改某些内容时,我都必须在所有子应用程序中进行更改。

I've been thinking about using target for this (every time I create a new one, I use a new target with a new folder images linked to it), but I'm not sure about this as I never worked with targets before. 我一直在考虑为此使用目标(每次我创建一个新目标时,我使用一个新目标,并将新文件夹图像链接到它),但我不确定这一点,因为我以前从未使用过目标。

Is this the best approach? 这是最好的方法吗?

And if it is, how is the best way of doing it? 如果是,那么最好的方法是什么? I created a new target inside my project, but it creates a whole new project inside of it, with its owns classes. 我在我的项目中创建了一个新目标,但它在其中创建了一个全新的项目,其拥有类。

For your case, using multiple targets is a good solution. 对于您的情况,使用多个目标是一个很好的解决方案。 For each target you'll include all sources, and only the images used by that target (images can have the same name, just put them in separate folders). 对于每个目标,您将包括所有源,并且仅包含该目标使用的图像(图像可以具有相同的名称,只需将它们放在单独的文件夹中)。

On Xcode 4, you'd need to follow these steps: 在Xcode 4上,您需要按照以下步骤操作:

  1. Go to Project settings, right click on your main app target and choose 'Duplicate'. 转到项目设置,右键单击主应用目标并选择“复制”。 This will already include same sources from the main app. 这将包含来自主应用程序的相同来源。

  2. Unlink all images that you don't need in your new target: 取消链接新目标中不需要的所有图像:

    • select all images that you want to unlink, and in the utilities panel (usually placed on the right), in the 'File Inspector' section, uncheck your new target 选择要取消链接的所有图像,并在“实用程序”面板中(通常位于右侧),在“文件检查器”部分中,取消选中新目标
  3. Create a new folder in which you place the images for your new target/app, and when you import them in the project, choose to link only with your new target. 创建一个新文件夹,在其中放置新目标/应用程序的图像,并在项目中导入图像时,选择仅链接新目标。

  4. When you duplicate your target, it will also automatically create a new info.plist for your new app. 当您复制目标时,它还会自动为您的新应用创建新的info.plist。 You'll need to change the bundle id, and any other options that you wish. 您需要更改包ID以及您希望的任何其他选项。

Another option would be to make the main project as a static library, and in all other projects include the main project, and link the target. 另一种选择是将主项目作为静态库,并在所有其他项目中包括主项目,并链接目标。 Won't get into this, since the targets solution seems better for your case, for only changing the images. 不会涉及到这一点,因为目标解决方案似乎更适合您的情况,仅用于更改图像。

I wish to add a link showing howto - in support of the above answer. 我希望添加一个链接,显示如何 - 支持上述答案。 Hope it will be helpful to others also. 希望它对其他人也有帮助。

iOS Mobile Development: Using Xcode Targets to Reuse the Code iOS移动开发:使用Xcode目标重用代码

http://www.itexico.com/blog/bid/99497/iOS-Mobile-Development-Using-Xcode-Targets-to-Reuse-the-Code http://www.itexico.com/blog/bid/99497/iOS-Mobile-Development-Using-Xcode-Targets-to-Reuse-the-Code

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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