Xcode "passing variables at launch" question
When I select the executable in the Xcode project interface, an Info window comes up containing information on 开发者_开发知识库the executable. When I select the arguments tab, I enter some "variables to be set in the environment." How would I access these variables?
Edit: Upon re-reading your question, you're actually talking about environment vars. See the last paragraph for how to read those. If you use the other table on that same tab, "Arguments to be passed on launch", then the first three paragraphs are more applicable.
Variables passed this way will be available in the argv
array in main()
. If you want to access them later without modifying main()
, you have a couple of options.
You can use [[NSProcessInfo processInfo] arguments]
to get an array of all arguments passed on the command-line. This will give everything passed, verbatim.
You can also use NSUserDefaults
. Arguments passed on the command-line in pairs as -name value
will set the NSUserDefaults key name
to have the value value
. This will override any variables of the same name loaded from the actual application preferences. Note that this is two args, not one arg with a space.
Either of these approaches should work for you. A third option is to not use command-line arguments but instead to use environment vars, which can be accessed via [[NSProcessInfo processInfo] environment]
, but whether this makes more sense than arguments is up to your particular use case.
精彩评论