Differences between project and targets in xcode

Source: Internet
Author: User

In the xcode project, targets is the content to be introduced in this article. I believe many people have noticed the concept of target in xcode. this is reflected in many places. For example, after opening a project, there is a targets item in the list on the left, and in the top menu of the Project interface,
There are also multiple projects that involve the target, so what is the target?

Apple said: "targets that define the products to build. A target organizes the files and instructions needed to build a product into a sequence of build actions that can be taken."

In a simple understanding, we can think that a target corresponds to a new product (based on the same Code). But all of them have a piece of code. What should we do with a new product? Is this interesting?

In fact, this is not just a waste. Although the Code is the same, compilation settings (such as compilation conditions) and the resource files contained can be very different. therefore, even if the same code is generated, the product may be very different.

Let's take a few typical applications with multiple targets, such as the full version and Lite version; for example, the 20-off, 30-off, and 50-off versions of the same game; or, for example, changing resources and names for the same game can be sold as new games (hello, what are you teaching ...)

What are the same and what are the differences between targets?

Since the same code is used to generate different products, what are the differences between different targets?

To solve this problem, let's take a look at the content specified by a target.

From the list on the left side of xcode, we can see that a target contains copy bundle resources, compile sources, link binary with libraries.

Copy bundle resources refers to the resource files included in the. app of the generated product.

Compile sources indicates which source code is compiled

Link binary with libraries indicates which library files will be referenced during compilation

By using different settings in copy bundle resources, we can make different products contain different resources, including the main icons of the program, etc, instead of including the resources listed in xcode projects.

This is not all the content specified by a target. Each target can use an independent, different info. plist file.

We all know that this info. plist file defines many key content of an iPhone project, such as the program name and the globally unique ID of the product.

In addition, different targets can define complete and differentiated compilation settings, from simple adjustment of Optimization Options to adding the compilation conditions used for Conditional compilation, so that the base SDK can be differentiated.

Create the second target

Why is it the second one? Because the first one is the default target after the project is created! (So much nonsense, drag it away ...)

There are multiple methods to create a target. We can copy a copy from the existing target, and then slightly Add the changes. We can also create a new target. But actually, the two methods are similar.

First, let's take a look at how to create a target using the replication method.

Create a Target Using Replication

In the list on the left side of xcode, expand the targets item. On the existing target, right-click and select "DUPLICATE", or select an existing target, in the top menu, select "DUPLICATE.

Now we get a new target, and a XXXX copy will be obtained in the resource. plist. this new target is exactly the same as the original target, and the rest is some differentiated modifications. Let's talk about this later.

Create a new target

Similar to the copy method, we can right-click the menu in many places in the list on the left, and we can see that "new target... "item, and in the project on the top menu of the project, you can also see this" new target....

After clicking this button, we will first let you choose the target type. Since I have always been referring to the program itself, we will naturally choose application. (for other items, please study them as you are interested, for example, we can extract parts of the program into a static library ).

Next, you will be asked to enter a new target name, instead of generating a target name such as XXXXX copy by default in the copy method.

However, the generated target is almost empty. Copy bundle resources, compile sources, and link binary with libraries does not contain any content. The compilation settings are completely original.

You can drag the content to the target settings and adjust the compilation options to complete the target configuration.

How to modify some content in target

In fact, this part of content may also be used in non-multi-targets projects.

Since the modification is basically completed in the project/compilation settings, no declaration is made in special cases. You can right-click the target to open the corresponding project/compilation settings, select get info.

Modify the generated product name: the product name in the packing segment

Info. plist file name: info. plist file in the packing segment. For example, if the copied target finds that XXXXX copy. plist is too silly, you can change it here.

Compile items: Add a user-defined setting (the gear in the lower left corner of the build page of target "XXXX" info to see this content), and enter it in other C flag, for example, to define a define value called lite_version, we can write "-dlite_version" or "-dlite_version = 1 ". then it can be used in the program

  1. #if defined(LITE_VERSION)   
  2. #else   
  3. #endif  

Such Conditional compilation is used to partially differentiate the code.

Some may remember the cracked version of the code I posted in the code area. One of the methods is to check info. plist is text or binary, so can we recommend a simulated cracking target to directly generate text info. plist for testing?

Of course you can. In the packing section, there is an item called "info. plist output encoding ". The default value is binary. We only need to select XML to generate the product. info. plist is a text style.

In addition, to add/delete files in copy bundle resources, compile sources, and link binary with libraries, you can select get info on the file to be modified and switch to the target page, select the target to reference this file. for example, icon.png can be specified to the default target, while icon_lite.png can be specified to the target of lite verion.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.