“This is the third day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

What is an XCConfig file

Simply put, xcConfig is a configuration file that Apple uses in Xcode files to enable the same set of code to compile and run under different conditions.

How to generate and configure a configuration file

Add a new file to the project, then search for Configuration in the pop-up box and you will see the following result:

I am here to create a named TestConfig. Debug. Xcconfig file

Then set up the newly created file in Project so that it can be used under Debug conditions

Now you can start in TestConfig. Debug. Xcconfig add configuration file.

xcconfigFile Syntax introduction

The syntax of the XCConfig file is relatively simple, and each configuration file consists of a series of key values

BUILD_SETTING_NAME = value
Copy the code

annotation

The xcconfig file has only one comment method \\

includeImport other Settings

As you create the XCConfig file, you can create as many as you need. At this point, you can import configuration from other XCConfig using the include keyword

#include "Debug.xcconfig"
Copy the code

When importing files, you can write absolute paths or use relative paths. ${SRCROOT} represents the project root directory, and $(PROJECT_DIR) represents the entire project.

variable

Variable definitions, named after OC, consist only of uppercase letters, numbers, and underscores, uppercase in principle, but lowercase as well. Strings can be double quoted “or single quoted”. Variables have three special cases:

  1. inxcconfigDefined variables andBuild SettingIf the variables are the same, then the overwrite situation will occur and can be passed$(inherited)Keyword to inheritBuild SettingThe most common one is to solve the sumpods.xcconfigIn the conflict.
OTHER_LDFLAGS = $(inherited) -framework "AFNetworking"
Copy the code

Some variables cannot passxcconfigConfiguration toBuild Settings, such as configurationPRODUCT_BUNDLE_IDENTIFIERDon’t take effect

  1. Reference variables and {} and () will work

  2. Conditional variables, conditional according to SDK, Arch, and Configration, for example:

 // Specify 'Configration' as' Debug '
 // Specify whether 'SDK' is an emulator, or 'iphoneOS *' macosX * 'etc
 // Specify that the effective schema is' x86_64 '
 OTHER_LDFLAGS[config=Debug][sdk=iphonesimulator*[arch=x86_64]= $(inherited) -framework "SDWebImage"
Copy the code

In Xcode 11.4 and later, you can use default to specify the default value when a variable is null

$(BUILD_SETTING_NAME:default=value)
Copy the code

Priority (from highest to lowest)

  • Manually configure Target Build Settings

  • The xcConfig file configured in Target

  • Manually configure Project Build Settings

  • The xcConfig file configured in Project

Xcode Build Settings for the XCConfig variable, see the link: xcodeBuildsettings.com

Conclusion:

There are a lot of Build Settings options that need to be set in the xcConfig file. If you set them in the xcConfig file, you can see them clearly and set conditional variables. As we configure multiple environments can be achieved through this way: specific implementation cases can refer to: iOS configuration of multiple environments three schemes