Edward Loveall Edward Loveall - 24 days ago 14
Swift Question

How to I access argv and argc in swift?

I'm writing a command line app using Swift but I'm not sure how to get access to the arguments passed in. In C/Obj-c it seems relatively painless but if I try:

argv[0]


I get an error: Use of unresolved identifier 'argv'

Answer

There are two ways to do this:

  1. The constants C_ARGC and C_ARGV. They are analogous to the argc and argv you'd see in a main() function.
  2. The CommandLine.arguments array will contain all of the arguments to the process. (In Swift 2, CommandLine was called Process)
Comments