Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare static member variables like Java's in Objective-C

How can I make an Objective-C class with class-level variables like this Java class?

public class test
{

    public static final String tableName = "asdfas";    
    public static final String id_Column = "_id";
    public static final String Z_ENT_Column = "Z_ENT";

}

I want to access them without making an instance, like:

String abc = test.tableName;
like image 722
Awais Tariq Avatar asked May 02 '13 05:05

Awais Tariq


People also ask

What is a static variable in Objective-C?

In both C and Objective-C, a static variable is a variable that is allocated for the entire lifetime of a program. This is in contrast to automatic variables, whose lifetime exists during a single function call; and dynamically-allocated variables like objects, which can be released from memory when no longer used.

How do you declare static variables?

Static variables can be accessed by calling with the class name ClassName. VariableName. When declaring class variables as public static final, then variable names (constants) are all in upper case. If the static variables are not public and final, the naming syntax is the same as instance and local variables.

How do I declare a global variable in Objective-C?

Somewhere in your header, you would declare a global variable like this: extern int GlobalInt; The extern part tells the compiler that this is just a declaration that an object of type int identified by GlobalInt exists.

Can static variables be reinitialized?

We can also initialize the value of the static variable while declaring it. The syntax for initializing the value of the static variable in C programming language is given below. Note: The value of a static variable can be reinitialized wherever its scope exists.


2 Answers

It looks like you want to create constants (since you are using final in your question). In Objective-C, you can use extern for that.

Do something like this:

1) Create a new Objective-C class named Constants.

2) In the header (.h) file:

extern const NSString *SERVICE_URL;

3) In the implementation (.m) file:

NSString *SERVICE_URL = @"http://something/services";

4) Add #import "Constants.h" to any class where you want to use it

5) Access directly as NSString *url = SERVICE_URL;


If you don't want to create constants and simply want to use static in Objective-C, unfortunately you can only use static in the implementation (.m) file. And they can be accessed directly without prefixing the Class Name.

For example:

static NSString *url = @"something";

I hope this helps.

like image 188
Avtar Guleria Avatar answered Oct 17 '22 19:10

Avtar Guleria


Try it....

static NSString *CellIdentifier = @"reuseStaticIdentifier";

You can access direct value using synthesis property
or you can use NSUserDefaults for store and retrive value

Description

@interface MyClass : NSObject
+(NSString *)myFullName;
@end

Implementation :

#import "MyClass.h"

@implementation MyClass
static NSString *fullName = @"Hello World";

+(NSString *)myFullName{
  return fullName;
}
@end

Use:

#import "MyClass.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
  NSLog(@"%@",[MyClass myFullName]); //no instance but you are getting the value.
}

@end

Hope i helped.

like image 31
Chirag Pipaliya Avatar answered Oct 17 '22 19:10

Chirag Pipaliya