Mastering macOS Programming
上QQ阅读APP看书,第一时间看更新

Using scheme environment variables

In the scheme editing window, select the Arguments tab.

The Arguments tab shows two empty lists, Arguments Passed On Launch and Environment Variables. The former we will leave for a later chapter; for now, we will concentrate on the environment variables.

The environment variables listed here are basically a dictionary of key-value pairs that are available to the app's code via the ProcessInfo.processInfo.environment dictionary, which we'll add to the app shortly:

  1. Click the + button to add an environment variable.
  2. Name the new environment variable Currency Symbol.
  3. Set its Value to £ or some currency symbol of your choice (New Zealand gave up its £s for $s many years ago). You must include the double quotes.
  4. Close the window.

If you run the app again, you'll see that the currency has changed:

This change is baked into the code in by the compiler. By testing the value of any environment variable key, we can make appropriate adjustments in the content of our code. There are various ways we could have done this, but adjusting a scheme's environment variables has got to be the simplest.

You might like to take the time now to browse through the scheme editor window's other tabs, Options, Diagnostics, and Info (the last of which we are about to use).

If you collaborate with other developers using a source control repository, you'll need to add the scheme to version control by ticking the Shared box in either the scheme editor window, or the manage schemes window, both reachable from the schemes list.

Any changes you want to make within the code can be covered using environment variables. The test is trivial to write and takes place at compile time. But what if we want to make changes to, say, the app's Build Settings, or even its Info.plist file? Can we do that too?

We can indeed, but not by using the scheme's environment variables. We now need to enlist the help of another one of Xcode's palette of development-sweetening goodies: build configurations.