there is something I try to understand. I have this object declaration in my AppsController-Class for a Cocoa Application
NSMutableArray *personArray;
In can put this declaration inside the Header file or inside the implementation of the class in the code file. It makes no difference. I can even put it outside the @implementation context right under the #import commands.The Application works just fine.
Since I don’t inherit from the AppsController Class or do anything else fancy with it I wonder what might be the difference between these types of declarations?
Where does the declaration really belong?
It depends on how you want to use the variable. If you place the variable declaration inside the interface for your class, each instance of your class will have its own copy of the variable, which is kept separate from all other instances of your class:
Each instance of the
AppsControllerclass will have its own copy of thepersonArrayvariable, which is separate from all other instances of the class.If, however, you define the variable outside of the interface, it becomes a global variable, and it is a shared (instances of your class do not get their own copy), and can be accessed from any instance of your class. If you declare it in the header as such:
it is also visible to methods in other files and classes that include your header.
If you declare the variable in the implementation file, but outside of the implementation itself, and prefix it with the
statickeyword, the variable will only be visible to the implementation of your class. This is common when you want a variable that is visible to you all of class instances, but not visible to anyone else, and is a way to create class variables.Since your object is a controller object, I am guessing that you only have one instance of it present in your application. You should declare the variable either:
personArrayvariable needs to be unique to each instance of your controller class (even if you only have one instance now, you may have more than one instance of it in future).statickeyword) if you want the variable to be visible to all instances of your class, with only one, shared instance of the variable.