In most of the newer object oriented languages you either have Defines and no Globals, or no Defines and no Globals.
Basically, in order to call an int from a singleton class you either need it to be a property of the class, or make it so you have a mutator and accessor methods for the private variable. Technically, if you don't want the object to be a singleton, then you could technically make the one variable in the class be a static variable, which means it is shared across all instances of the class. Thus, that one variable is only created once. However, you would still need to set a property to access it, or create mutator and accessor methods to access it.
Examples - Note this is all pseudo code:
If you want other objects to have only readonly access to the variable as a property then you can do:
@interface someObject : NSObject
{
int _myint;
}
//Note the property does not have a _
//The one with an under score is a private variable
//If you want it to be writable and readable then change
//(readonly) to (readwrite), (readwrite) not recommend for
//Object pointers
@property (readonly) int myint;
@end
@implements someObject
@synthesize myint=_myint
-(id) init
{
//initialize
_myint = 20;
}
@end
Then in another class you would do:
someObject *myobject = [[someObject alloc] init];
int someInt = myobject.myint;
//someInt now equals 20;
//Note you cannot do myobject.myint = 30
//It is readonly access
Don't want to create a property for a variable? Then do it the java way with accessor and mutator methods:
@interface someObject : NSObject
{
int _myint;
}
-(void) setMyInt:(int) value;
-(int) getMyInt;
@end
@implements someObject
-(void) setMyInt:(int) value
{
_myint = value;
}
-(int) getMyInt
{
return _myint;
}
@end
Now, if you just want a variable that cannot be changed but accessible to any object or method if including the .h. Then you could in the .h do a #define myint 20 or something like that. However, you cannot directly change myint as it is defined as 20.
If, you need variables / objects to be accessible to multiple objects at the same time, then I recommend creating a singleton. That way you can just include the .h of the singleton and call something like [MySingleton sharedSingleton].myint or [[MySingleton sharedSingleton] getMyInt].
I guess you could also go the C++ or C route and do variables in the .h files as static int myint that does not belong to any classes / @interfaces. That way if I recall, any class that incorporates that .h will have access to the static myint. I am not positive about that though...