18

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;
jscs
  • 63,694
  • 13
  • 151
  • 195
Awais Tariq
  • 7,724
  • 5
  • 31
  • 54

6 Answers6

38

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.

viz
  • 1,247
  • 16
  • 27
Avtar Guleria
  • 2,126
  • 3
  • 21
  • 33
  • extern are not treated as best way of programming. – Anoop Vaidya May 02 '13 at 06:14
  • The `const` in `const NSString *` is not meaningful. you are not allowed to manipulate the object pointed to by the pointer directly anyway – newacct May 03 '13 at 05:48
  • @AnoopVaidya: what's wrong with `extern`? In the original Java code, those variable are specifically `public`, which means they should be usable from other files. `extern` is exactly what allows this. – newacct May 03 '13 at 05:50
  • @newacct: I would prefer to share a data throughout the application by means of SharedInstance/Singleton class – Anoop Vaidya May 03 '13 at 05:53
  • Hey I made a gist some time ago, not ARC compatible but it can actually be used in ARC by using the assign methods and specifying strong or weak references. It's a bit closer to what the original question asked: https://gist.github.com/darionco/03649feeee57f86fee3f – Dario Sep 05 '14 at 03:41
31

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.

ymutlu
  • 6,585
  • 4
  • 35
  • 47
Chirag Pipaliya
  • 1,281
  • 12
  • 20
10

You need to use a class method to access anything that can be called without making an instance.

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

Implementation :

#import "MyClass.h"

@implementation MyClass
   static NSString *fullName=@"anoop vaidya";

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

How to use:

#import "MyClass.h"

@implementation AppDelegate

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

@end
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
2

Objective-C doesn't have class variables

I would recommend putting the static NSString in the implementation file of your class, and provide class methods to access it

@implementation MyClass

static  NSString* str;
Linga
  • 10,379
  • 10
  • 52
  • 104
2

Could be done like that:

@interface Test
 {
   static NSString *tableName;
 }

+(NSString *) getTableName;
@end

@implementation Test
+ (NSString *)getTableName
 {
    return tableName;
 }
@end

And then you fetch it:

NSString *name = [Test getTableName];
Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
2

I think the best way and more used is to use enums such as

enum{
    GSPAYMENT_METHOD_CARD = 1,
    GSPAYMENT_METHOD_CASH = 2,
    GSPAYMENT_METHOD_VOID = 3
};
typedef NSUInteger PaymentMethodType;

Just before the @interface GSPaymentMethod

This way you can use those constants anywhere by just including the .h file

For example

[self newPayment:GSPAYMENT_METHOD_CASH]

Jordi Puigdellívol
  • 1,710
  • 3
  • 23
  • 31