-1
public class CommandType {
    public static final int DELETE = -1;
}

//Class B - Access from class B here
CommandType.DELETE

If i am using

//ClassA.h
extern int  const kMyConstant;

//ClassA.m
int  const kMyConstant = @"my constant"; 

switch (messagetype) {
                case kMyConstant: //Can't set const value here
    }

I need to convert this to objectiveC. Is it possible to do?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
SampathKumar
  • 2,525
  • 8
  • 47
  • 82

2 Answers2

2

Try this in your .m file if you want to use this in your class.

#import "yourimport";
static const NSInteger DELETE = -1;
@implementation YourClass

If you want that it would be global variable you should do this in.h file

extern NSInteger *const DELETE;

In order to do this

//ClassA.h
extern int  const kMyConstant;

//ClassA.m
int  const kMyConstant = @"my constant"; 

switch (messagetype) {
                case kMyConstant: //Can't set const value here
    }

You should create ENUM in .h:

#import <AVFoundation/AVFoundation.h> 
typedef NS_ENUM(NSInteger, YourType) {
    YourTypeConstant1                      = 0,
    YourTypeConstant2,
};

@interface YourViewController : ViewController

And then:

NSNumber *number = @(YourTypeConstant1);
switch (number) {

        case YourTypeConstant1:

            //your code
            break;

        case YourTypeConstant1:

           //your code
           break;
default:
//your default code
break;}
1

The best way to handle a integer constant is to declare it in a .h file:

static const NSInteger DELETE = -1;

Then every file (in your case e.g. Class B) that imports the .h file will be able to access the constant e.g.:

NSInteger test = DELETE;

That is the closest you will get to the java code...

David Silverfarmer
  • 455
  • 1
  • 3
  • 13