Where various types of variables and types should be placed in the header

I noticed that I get compilation errors if I put certain ads in certain places in my header file. I put comments in code where I think some things go; are they correct

@interface Level : CCNode { //Instance variables? PlayBackgroundLayer* playBGLayer; PlayUILayer* playUILayer; PlayElementLayer* playElementLayer; } //Static methods? +(void) InitLevel: (int) levelNumber; +(Level*) GetCurrentLevel; //Property declarations for instance variables? @property (nonatomic, retain) PlayBackgroundLayer* playBGLayer; @end //Static variables? Level* currentLevel; PlayTilemapLayer* playTilemapLayer; 
+1
source share
2 answers

You are correct in all calculations except one. Your last variables are not static variables, they are global variables. Static variables are simply variables declared with the static keyword, and they mean something slightly different from other languages. They are not class variables, they are variables that are visible only to the file in which they were declared, and only then in the area in which it was declared (if you declare it inside a function, other functions will not see it). However, as you would expect, they are declared once no matter how many copies you have. If you declare something outside the interface without a static keyword, like you, other classes will import them. However, this is not an ideal way to accomplish this (you may get overriding errors if more than one class imports this header).

In addition, one caveat is that properties do not need to have an explicit reference variable (the compiler will create it for you if you use the @synthesize keyword), but, of course, if you want this, there is nothing wrong with that.

Finally, you should notice that the only reason your static methods are not instance methods is because they start with a plus (+) symbol, as opposed to a minus (-) .

+1
source

Instance variables usually do not need to be explicitly specified. They are created when you @synthesize property. If you really want them, then the (new) right place * is at the top of the implementation block:

 @implementation Level { PlayBackgroundLayer* playBGLayer; PlayUILayer* playUILayer; PlayElementLayer* playElementLayer; } 

These are not static methods, these are class methods , but yes, this is where you declare them. Some people like @property declarations before classes, but this is a matter of opinion. Instance methods come after both of them, although technically speaking the order doesn't matter - that is, the compiler doesn't care, it's just a matter of readability.

However, these top-level variables should go somewhere other than the header file. If you put them in there, you will get compilation errors, because every file that imports the header seems to re-declare storage for these variables, which is not allowed.

Usually you put such variables in a .m file. If you want them to be visible only there, you would use static . If you want them to be visible from other files that import the header, you leave static turned off and declare the variable as extern in the header:

 extern Level* currentLevel; 

This allows the compiler to know that the storage for the variable is reserved elsewhere.


* See "Class Interface" in TOCPL.

+1
source

All Articles