Take the 2-minute tour ×

I want to create a special class for holding a lot of values like strName, strNo, intPassword...So that several other classes can modify the same value in this class. I want to use the dot syntax to call its properties like

Values.strName = ...; 
I don't want to initialize the class everytime before I read/write its values because some of its values are still in use.

I call the class 'Values' and design it using the following code:

@interface Values : NSObject {
    IBOutlet NSString *strName;
@property (nonatomic, copy) NSString *strName;

I import "Values.h" to another class and call the property using

Values.strName = @"Name";

I don't create an object of 'Values' to avoid the data be overwritten. An error pops up saying that 'syntax error before . token'. Any other ways to create a class storing global variables but needn't be initialized when using these data?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

Another solution to thos problem is to add the variables (or classes) to the AppDelegate object. It's easy for the AppDelegate to become a ball-of-fluff but if these variables really are propertiers of your app, it's the right place for them to go.

You can always get a reference to your AppDelegate using [UIApplication sharedApplication].delegate (you may need to cast this)

share|improve this answer
Good idea! This may be the simplest way to store global variables. Thanks. –  iPhoney Mar 11 '09 at 8:18
Another way, at least on the Mac, would be to subclass NSApplication and put the properties there. I don't know whether you can do that with UIApplication. –  Peter Hosey Mar 11 '09 at 9:50
Good suggestion. After some attempts, I find that I can declare a new class just like the delegate class to store global data. Thanks. –  iPhoney Mar 12 '09 at 6:29


Why? There's nothing wrong with putting immutable and primitive objects in read-only global variables:

NSString *const strName = @"Name";
const NSInteger intPassword = 0xDEADBEEF;

If you want to prefix them as being globals, “g” (as in gStrName, etc.) is common and works fine.

You may be tempted to say “but globals are evil!”, but think about what you're saying: Why are globals evil?

  • Because other code can change the objects. (Not so with immutable objects.)
  • Because other code can replace the objects. (Not so with read-only (const) variables.)
  • Because two pieces of code can use the same object. (Not a problem if the object is basic, such as a string or integer.)

As long as none of these complaints applies, there is nothing wrong with global variables. They are quite common in Cocoa (most of its string constants, for example, are actually global variables).

share|improve this answer
The reason why I want to create this class is that this class is used to let other classes read and write its value(several classes may modify the same value). I forget to mention:these values are not const. –  iPhoney Mar 11 '09 at 7:06

If you use a singleton pattern, then you can achieve something close to this. Using the 'SynthesizeSingleton.h' helper by Matt Gallagher:

// Values.h
#import "SynthesizeSingleton.h"

@interface Values : NSObject {

// props

+ (Values *)sharedValues;

// Values.m
@implementation Values


@synthesize props;


Then to use your singleton:

NSString *myVal = [[Values sharedValues] someValue];


Values *v = [Values sharedValues];

NSString *myVal = v.someValue;

share|improve this answer

Your Answer


