iosxcodexcode6

how to define strings in xcconfig and quotes


I've started using an xcconfig file for environment specific build settings and I noticed that string quotes are literally interpreted.

e.g.

APP_BUNDLE_DISPLAYNAME_SUFFIX = "DEBUG"

will show an app name of MyApp "DEBUG" as the display name (with the quotes)

How do you handle strings in an xcconfig file, are they necessary to define strings? If not how do you deal with empty spaces and escaping? Any special characters to e aware of?


Solution

  • The answer is that it depends on the actual build setting you're trying to change, and where it gets used.

    As you've observed, a setting like APP_BUNDLE_DISPLAYNAME_SUFFIX can only take a single word, and so adding quotes here just results in them being incorporated by Xcode into the value of the setting.

    However, in other places it is different. Particularly, if a setting is passed onto the toolchain during the build. In these cases, you have to use quoting and escaping for both Xcode AND the command-line.

    For example: if you want to use an xcconfig file to set preprocessor definitions (i.e. macros) and you want to define a string macro, you have to escape the quotes so that the shell doesn't strip them, and if you have slashes in the string then you also have to escape these because Xcode interprets them internally.

    Suppose in your code you have:

    #ifdef MY_MACRO1
    const char *my_url = MY_MACRO2;
    #endif
    

    where MY_MACRO2 should be set to a quoted string "https://stackoverflow.com/questions/30926076/", then this xcconfig file will work correctly:

    //
    //  my_string_macro.xcconfig
    //
    
    GCC_PREPROCESSOR_DEFINITIONS=MY_MACRO1 MY_MACRO2="\"https:\/\/stackoverflow.com\/questions\/30926076\/\"" 
    

    Xcode's Build Settings inspector will show the value between the outermost two double quotes unchanged, but it will be correctly interpreted during compilation, leading to something like this in the compile log:

    clang -x objective-c++ -DMY_MACRO1 -DMY_MACRO2=\"https://stackoverflow.com/questions/30926076/\"