preface

During the development process, we will definitely encounter the configuration of the development environment and the formal environment, such as: server address, some encrypted keys, etc., will distinguish between the test environment and the formal environment;

Structures, testing, formal environment, the operations group is good, maybe there are some company has set up a DNS for the test environment (or wifi) test environment, users manually switch switch (or wifi), DNS address to access the test environment, this is the best, but not all companies are so “good”, many still need to develop their implementation;

Practice 1 (Low)

One practice (not recommended) is to define two macros in code as follows:

Commenting code to switch the environment can also do this, manual switching is error-prone, not recommended;

Practice 2 (Good)

In order to solve the problem of manual modification, it is defined through the Debug mode judgment, which is used to modify the code;

Often, the result of this approach is your.h file, which looks and feels cluttered, unsightly, and has not yet been fully decoupled;

Practice 3 (Better)

Recently, a method of setting the decoupled project was found in the development process: Configuration Settings File (.xcconfig)

You can create a custom.xcconfig configuration file in Xcode, and then select the corresponding.xcconfig configuration file in Debug mode and Release mode of the project, so that you can achieve the separation of Dubug code and Release code maintenance, perfect!

Specific operation

1. Create a.xcConfig config file:

Xcconfig, debug. xcconfig, and release. xcconfig. Common is Common. Debug and Release are independent of each other.

Configurations 2. After you have created the file, go to PROJECT — > Info — >Configurations and configure the.xcconfig configuration file for Debug and Release respectively, and select the.xcconfig file name

Define key/value pairs in debug. xcconfig and release. xcconfig respectively. Expose key pairs in common. xcconfig so that they can be called directly. Ps: In each mode, the key name must be consistent.

GCC_PREPROCESSOR_DEFINITIONS = $(Inherited) GCC_PREPROCESSOR_DEFINITIONS = $(Inherited)Copy the code

GCC_PREPROCESSOR_DEFINITIONS = $(Inherited)

Code calls

Running results:

In Debug mode:

Release mode:

Done!!

Attached is the demo for configuring xcConfig: XcodeConfigDemo

Additional:

Xcconfig is used to save a plain text file of the build setting key-value pairs, which override the values in build Setting as follows:

ENABLE_BITCODE = NO
MACH_O_TYPE = staticlib
VALID_ARCHS = arm64 armv7
IPHONEOS_DEPLOYMENT_TARGET = 7.0
Copy the code

Play according to your own project needs!


Practice is the sole criterion for testing truth…