C.Johns
C.Johns

Reputation: 10245

how to declare constants in objective c

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

Upvotes: 0

Views: 1038

Answers (2)

Denis
Denis

Reputation: 6413

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.

Upvotes: 2

Rob Napier
Rob Napier

Reputation: 299275

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.

Upvotes: 5

Related Questions