I want to define a constant file path that includes whoever’s username (e.g. /Users/username/Desktop…in my specific case it’s a directory I create at /var/spool/FolderICreate/username).
What’s the best way of declaring this constant so my other classes can recognize it? I currently have a globals.h file to include for the classes that need to see the globals, but I’m not sure how to set the value. #define obviously needs a hardcoded string literal, and I’m not sure if I can or how I would set the string using extern const NSString*. I feel like this shouldn’t be hard, but I’m at a loss.
— EDIT —
As people have rightly pointed out, my code was unnecessary because I can get the username with NSUserName, etc, so I’ve removed it. But I think people are missing the point of my question. I can see there are multiple ways to get the pathname I want–how do I declare that as a constant?
Have a look at NSPathUtilities before you re-implement a bunch of functionality that’s already provided in Foundation framework. You’ll find a bunch of methods there specifically for what you’re trying to do.
To answer the question you actually asked, there’s no straightforward way to declare a variable as a constant and then change its value at runtime… That sort of defeats the purpose of telling the compiler it’s constant. Simply declare the variable in a header file as an
extern NSString *and only set it once. 🙂