the apple dev docs has got some useful info on static variables:
When you define a new class of objects, you can decide what instance
variables they should have. Every instance of the class will have its
own copy of all the variables you declare; each object controls its own
data.
However, you can't prescribe variables for the class object; there are
no "class variable" counterparts to instance variables. Only internal
data structures, initialized from the class definition, are provided for
the class. The class object also has no access to the instance variables
of any instances; it can't initialize, read, or alter them.
Therefore, for all the instances of a class to share data, an external
variable of some sort is required. Some classes declare static variables
and provide class methods to manage them. (Declaring a variable static
in the same file as the class definition limits its scope to just the
class-and to just the part of the class that's implemented in the file.
Unlike instance variables, static variables can't be inherited by
subclasses.)
Static variables help give the class object more functionality than just
that of a "factory" producing instances; it can approach being a
complete and versatile object in its own right. A class object can be
used to coordinate the instances it creates, dispense instances from
lists of objects already created, or manage other processes essential to
the application. In the case when you need only one object of a
particular class, you can put all the object's state into static
variables and use only class methods. This saves the step of allocating
and initializing an instance.
Quote:
Originally Posted by neigaard
Im an old Java developer, staring on Objective-C. In Java I often declare some static variables to be used to test on later, like this:
Code:
public class Foo {
public static int TYPE_CIRCLE = 0;
public static int TYPE_SQUARE = 1;
public void draw(int type) {
if(type == TYPE_CIRCLE) {
...
}
}
}
And then I can use this from another class like this:
Code:
public class Bar {
Foo f = new Foo();
f.draw(Foo.TYPE_CIRCLE);
}
Can I do something like this in Objective-C, or what do you guys do to archive something like this?
Thank you
Søren
|