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:
@interface AppsController : NSObject
{
NSMutableArray *personArray;
}
Each instance of the AppsController
class will have its own copy of the personArray
variable, 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:
NSMutableArray *personArray;
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 static
keyword, 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:
- As an instance variable if your
personArray
variable 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).
- As a class variable (using the
static
keyword) if you want the variable to be visible to all instances of your class, with only one, shared instance of the variable.
- As a global variable if you want the variable to be a single instance (not unique to instances of your class) and also visible to other classes or code in other files.