0

I need to declare two different constants in my app one is a simple string, the other needs to be a uint32.

I know of two different ways to declare constants as follows

#define VERSION 1; //I am not sure how this works in regards to uint32.. but thats what I need it to be.

and

NSString * const SIGNATURE = @"helloworld";

is there a way to do the version which should be a uint32 like the nsstring decliration below?

for instance something like

UInt32 * const VERSION 1;

if so how? if not, how do i make sure the #define version is of type uint32?

any help would be appreciated

C.Johns
  • 10,185
  • 20
  • 102
  • 156
  • would [this related, potentially duplicate question](http://stackoverflow.com/questions/1674032/static-const-vs-define-in-c) have the answer you're looking for? – Michael Dautermann Feb 22 '12 at 21:05
  • possible duplicate of [Constants in Objective C](http://stackoverflow.com/questions/538996/constants-in-objective-c) – Krizz Feb 22 '12 at 21:07

2 Answers2

5

You're very close. The correct syntax is:

const UInt32 VERSION = 1;

You can also use UInt32 const rather than const UInt32. They're identical for scalars. For pointers such as SIGNATURE, however, the order matters, and your order is correct.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • cool, thanks very much for that, I will mark your answer once the time is up for doing so. pretty much that explains why I was getting the pointer error because I was using the *... damn i hate pointers. – C.Johns Feb 22 '12 at 21:09
2

You're confused by macro definitions & constants:

#define VERSION (1)

or

#define SOME_STRING @"Hello there"

The above are macro definitions. This means during compilation VERSION & SOME_STRING will be replaced with the defined values all over the code. This is a quicker solution, but is more difficult to debug.

Examples of constant declarations are:

const NSUInteger VERSION = 1;
NSString * const RKLICURegexException = @"Some string";

Look at the constants like simple variables that are immutable and can't change their values.

Also, be careful with defining pointers to constants & constant values.

Dan J
  • 16,319
  • 7
  • 50
  • 82
Denis
  • 6,313
  • 1
  • 28
  • 27