i would like to do some order to my self about the globals.
-
if i write in my .m file
int ran //is it global? every class can see it ? @implementation @end -
if i am writing in my .h file this:
int ran //is it global to any other class that include this .h ? @interface { } -
if i am writing in my .h file this:
extern int ran //is it global to any other class that include this .h ? @interface { } -
if i am writing in my .h file this:
@interface { int ran //can every other class that include it can see it ? }ANDabout the@interface, every thing that in the interface is belong to that class only?
what about*NSStringthat in the @interface? is it belong to that class only ?
can i use it when including this .h? or do i have to make an object of that class to see it ?
What is the best way to put a global variable of a primitive , that only my class will see it and can be use it ?
If you write (in any compilation unit == .m file)
Fine, defines a global variable, initialized to zero, symbol visible to the linker.
If you write in a header file
That’s a bad idea, since every implementation file (.m) that includes the header would define it’s own instance of the global variable. Linker confusion would arise.
If you write in a header file
Fine, declares a global variable. Must be defined in one implementation file (.m).
If you write in a header file
That’s ok, too. It declares an Objective-C class with an instance variable
ran. The instance variable would be created for and belong to each instance ofFoo. The instance variable is @protected (by default) and can thus be accessed directly from any deriving class. This would be bad design, though.Nowadays you would not declare instance variables in the
@interfaceof a class but in the@implementationor just@synthesizethem.If you need a private global variable, define it in you implementation file using: