Question

I am creating a command line tool in Xcode using the foundation framework and I am confused on the basics of dealing with file paths. The first argument is to be a file (myfile.txt) or file path (./myfile.txt). I want to turn this argument into an absolute path (or URL) for the file. How do I do this?

Was it helpful?

Solution

Use -[NSURL fileURLWithPath:].

int main(int argc, char *argv[]) {
  @autoreleasepool {
    if(argc < 2)
    {
      fprintf(stderr, "Usage: %s [file path]\n", argv[0]);
      exit(-1);
    }

    NSURL *f = [NSURL fileURLWithPath:[NSString stringWithUTF8String:argv[1]]];
    /* Do something with f */
  }

  return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top