I would like to use class variables outside of the class.
By 'outside', I mean the main function where I instantiate the class.
@interface Class: NSObject
static int var_class; // Class variable
{
int var_object; // Object variable
}
@end
@implementation Class
@end
In order to access var_object
, the object variable, we use:
int main(void)
{
Class *object = [Class new];
// Access to the variable
object->var_object;
(*object).var_object;
return 0;
}
I thought I could access var_class
, the class variable, in the same way.
But none of them worked. (Unable to compile)
Class.var_class;
// -> Generates error: could not find setter/getter for 'count' in class 'TheClass'
Class->var_class;
// -> Generates error: error: expected '.' before '->' token
How can I get/set the class variable's value?
Declaring a function is the way to access class variables, but I want it simpler.
@interface Class: NSObject
static int var_class; // Class variable
+(int) get;
+(void) set: (int) val;
@end
@implementation Class
+(int) get
{
return var_class;
}
+(int) set: (int) val
{
var_class = val;
}
@end