What the difference between external variable of some sort and static variable?
//ClassA.m
NSString *var1;
static NSString *var2;
@implementation ClassA
...
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
extern
An
externconstant/variable is one which one definition may be accessed (or referenced) across multiple object files. It’s a global, exported C symbol. Use this if you want access to the constant/global variable from multiple translation units (or compiled files) or if you want to use it in multiple binaries (e.g. you want to use it from your app, and the definition is in a dynamic library). You will typically declare this in the header for others to use.static
The
staticemits a copy for each translation. Each file that is compiled which also sees (e.g. is#included) the static will emit a copy of that static. You should avoid this. This will result in a bloated binary that has very confusing execution. You should favor the static if the value is local to a file, and should be private to that file.For these reasons, you should favor
staticin your .c, .m, .cpp, and .mm files, andexternin your headers.Finally, that
NSStringpointer should beconstby default:or
If you still want more, here’s a related answer I wrote, and here’s another.