Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access a user-defined Xcode build setting?

If I added a user-defined setting in my build configuration, how can I read that setting in my Objective-C code?

I have two files in my project, debug.plist and release.plist. I want my MainApp.m file to read one of these files based on which build configuration is running. I set up a user-defined setting named "filename" in both the Debug and Release configurations to point to the appropriate file. But I don't know how my MainApp.m file can read the filename variable from the current running configuration.

like image 420
The Lazy Hiker Avatar asked Oct 19 '10 13:10

The Lazy Hiker


People also ask

How do I get Xcode build settings?

Choose the project in the Project Navigator on the left. Select the Configurations target from the Targets section and click the Build Settings tab at the top. The Build Settings tab shows the build settings for the Configurations target. It's possible to expand this list with build settings that you define.

How do I add a user-defined variable in Xcode?

Select your project or target from the left side of the editor. Click the Build Settings button at the top of the editor. Click the Add Build Setting button at the bottom of the editor and choose Add User-Defined Setting. The original accepted answer, back in Xcode 4 days.

Where is target settings in Xcode?

To see detailed information about a particular setting, select the setting and choose View > Inspectors > Quick Help. Xcode displays a description of the setting, along with its name and value type.


2 Answers

Here's what I did, I'm not 100% sure if this is what you're after:

  1. Go into the build Settings panel and choose the gear icon in the bottom left: add User-Defined Setting
  2. Create your user defined setting, for example:

    MY_LANG -> en_us 
  3. Then, in the Preprocessor Macro's setting, you can reference that value:

    LANGCODE="$(MY_LANG)" 

Now you can refer to LANGCODE in all your source files, and it will be whatever you filled out in your custom build setting. I realize that there's a level of indirection here, but that is intentional in my case: my XCode project contains a bunch of different targets/configurations with their own preprocessor macro's. I don't want to have to go into all of those, just to change the language code. In fact, I define the language code on the project level. I also use MY_LANG in a couple scripts, so just a preprocessor macro wouldn't do. There may be a smarter way, but this works for me.

like image 85
zmippie Avatar answered Sep 18 '22 02:09

zmippie


You can access your user-defined build setting at run-time (as suggested in a comment by @JWWalker)

  1. Add an entry to your Info.plist file, and set it to your User-defined Build Setting

    MySetting -> ${MYSETTING} 
  2. Read its value from code

    Objective-C

    [[NSBundle mainBundle] objectForInfoDictionaryKey:@"MySetting"]; 

    [Edit] Swift

    guard let mySetting =    Bundle.main.object(forInfoDictionaryKey: "MySetting") as? String      else { print("MySetting not found") } 
like image 20
AndyDeveloper Avatar answered Sep 20 '22 02:09

AndyDeveloper