DCMaxxx DCMaxxx - 5 months ago 22
iOS Question

Objective-C : #define vs extern const

I know this question has been asked before, but I can't seem to find information about it in Apple's documentation; maybe some of you guys did.

A lot of Objective-C code has cross-file constants in a

.h
file, using
#define
.
Others use the approach of a
.m
with constants and
extern
them in the
.h
file.

I understand the difference, both pros and cons, but does Apple state which one to use in iOS development?

Answer

Apple's recommendation is extern:

Define constants for strings used for such purposes as notification names and dictionary keys. By using string constants, you are ensuring that the compiler verifies the proper value is specified (that is, it performs spell checking).

Admittedly they are inconsistent about this sometimes.

Comments