Quick Cocoa Question


cocoa love
hi my name is Roger and i have a question. Ummm when you declare instance variables in a header file and in the implementation file, (and i know that instance variables only come from a certain class.) Do the instance variables you declare have to be of a certain class and must that class must be defined too? Because in many forms i can think of, I don't need to use many objects except for one particular method, all feedback is welcome and appreciated.


Hi Roger,

Variables are always from a particular (somewhere else defined) type/class. This type/class has to be defined somewhere (Apple provides many predefined classes, but you can define your own)

You can roughly descibe this as follows:

Variables within an object:
You can define variables as instance variables in your class definition. In this case you will keep these instance variables during the lifetime of the object, giving the object sort of a 'memory'.

Variables within a method:
You can define them within some method of your class. In this case you will have the variables only during each one method call. After the call returns, the variables will 'disappear' and will be created again at the next method call.

Hope this helps,


So you can ask yourself the question:
Does myObject have to 'possess' or keep a 'reference' (track it for a longer period) of that variable => instance variable, or:
Does myObject only have to 'use' that variable => method variable.

Good luck!