Public Static Variables in Objective-C

Hi guys,

I am new to this plateform and I am working on a small iPhone application in Objective-C. Please let me how can I define the public static variables in objective-c and access these variables with object in other classes.

Please let me if anybody knows how can I define and access the public static variable in Objective-C.

Thanks,
Ranjit Singh

Mac mini, Mac OS X (10.5.3)

Posted on Jul 10, 2008 5:56 AM

Reply
Question marked as Top-ranking reply

Posted on Jul 10, 2008 8:02 PM

If by "public static" you mean class properties, you'll be sad to learn that Objective-C doesn't support them. What you do instead is write accessors to set and get the value of a module-wide static variable. Here's what you do:

1. In your header file, declare class methods to use as accessors:

@interface MyClass : NSObject
+ (NSString*)defaultName;
+ (void)setDefaultName:(NSString*)newDefaultName;
@end


2. At the top of your implementation file, declare a static variable to hold the value:

static NSString* MyClassDefaultName = nil;


3. In the implementation section for your class, write your accessors:

@implementation MyClass
+ (NSString*)defaultName {
return MyClassDefaultName;
}
+ (void)setDefaultName:(NSString*)newDefaultName {
if(MyClassDefaultName != newDefaultName) {
[MyClassDefaultName release];
MyClassDefaultName = [newDefaultName retain];
}
}


4. Finally, if you want to set a default value, you can use your class's initialize class method. (initialize may be called more than once, so make sure you check that you haven't already set the variable.)

+ (void)initialize {
if(!MyClassDefaultName) {
MyClassDefaultName = @"Default default name";
}
}
@end


Use [MyClass defaultName] to retrieve the variable, and [MyClass setDefaultName:newValue] to change the variable. Child classes will inherit these methods, but will not be able to set the variable directly; they will have to use the accessors.

Message was edited by: Brent Royal-Gordon

Message was edited by: Brent Royal-Gordon
3 replies
Question marked as Top-ranking reply

Jul 10, 2008 8:02 PM in response to ranjit.singh

If by "public static" you mean class properties, you'll be sad to learn that Objective-C doesn't support them. What you do instead is write accessors to set and get the value of a module-wide static variable. Here's what you do:

1. In your header file, declare class methods to use as accessors:

@interface MyClass : NSObject
+ (NSString*)defaultName;
+ (void)setDefaultName:(NSString*)newDefaultName;
@end


2. At the top of your implementation file, declare a static variable to hold the value:

static NSString* MyClassDefaultName = nil;


3. In the implementation section for your class, write your accessors:

@implementation MyClass
+ (NSString*)defaultName {
return MyClassDefaultName;
}
+ (void)setDefaultName:(NSString*)newDefaultName {
if(MyClassDefaultName != newDefaultName) {
[MyClassDefaultName release];
MyClassDefaultName = [newDefaultName retain];
}
}


4. Finally, if you want to set a default value, you can use your class's initialize class method. (initialize may be called more than once, so make sure you check that you haven't already set the variable.)

+ (void)initialize {
if(!MyClassDefaultName) {
MyClassDefaultName = @"Default default name";
}
}
@end


Use [MyClass defaultName] to retrieve the variable, and [MyClass setDefaultName:newValue] to change the variable. Child classes will inherit these methods, but will not be able to set the variable directly; they will have to use the accessors.

Message was edited by: Brent Royal-Gordon

Message was edited by: Brent Royal-Gordon

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Public Static Variables in Objective-C

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.