Search code examples
objective-cerror-handlingcompile-time-constant

How can PRODUCT_BUNDLE_IDENTIFIER be used as NSErrorDomain for app errors?


How can the product bundle identifier of an OS X app be used for an error domain? Are there any pitfalls of using it so?

It doesn't seem to be defined as a macro; I tried:

NSErrorDomain BSDomain = PRODUCT_BUNDLE_IDENTIFIER;

This results in an error:

Use of undeclared identifier 'PRODUCT_BUNDLE_IDENTIFIER'


Solution

  • It seems build setting variables are available on the command line, though not within source files. For any such variable, a preprocessor macro (see also "Add preprocessor macro to a target in xcode 6") can be defined manually based on the variable. The macro and setting can be given different names, but likely giving them the same name will make more sense.

    In this case, the value should start with an "@" and be surrounded by escaped double-quotes, so they're part of the macro and will be included when the macro is substituted. If either is left off, the macro value won't be a valid NSString constant (required for use as an NSErrorDomain value). The macro should be added to all configurations (Debug, Release, and any user-defined).

    PRODUCT_BUNDLE_IDENTIFIER=@\"$(PRODUCT_BUNDLE_IDENTIFIER)\"
    

    If the product bundle ID will also be used in other ways, the macro could be defined with the raw value (PRODUCT_BUNDLE_IDENTIFIER=$(PRODUCT_BUNDLE_IDENTIFIER)) with the appropriate sytax introduced at the usage sites:

    NSErrorDomain BSDomain = @"PRODUCT_BUNDLE_IDENTIFIER";