0

In my project i'm showing debug messages with global variable:

struct GVariables {
    static let debug = false
}


if GVariables.debug {
    print("Debug mode enabled")
}

But is it possible to set argument here:

enter image description here

and check debug argument in code. How can i do this ? And is this a correct way ?

Arti
  • 7,356
  • 12
  • 57
  • 122

1 Answers1

1

You can get access to those launch arguments and environment variables via NSProcessInfo

if NSProcessInfo.processInfo.arguments["DEBUGRPM"] ...

That's not unreasonable and allows you to change the behavior of a compiled app which can be useful in some cases. It does however have some overhead since you're always performing this check. If you would only ever enable debug logging in a debug build then setting a value in "Swift Compiler - Custom Flags" (as shown in the question @Larme linked) and using an #if DEBUGRPM expression will give you conditionally compiled code, saving the app the work of performing a runtime if test.

Which approach is more reasonable for you will depend on how you plan to use and when you plan to toggle this behavior.

Jonah
  • 17,918
  • 1
  • 43
  • 70