How to make constants
How to make constants in Objective-C?
Use define
It’s simple way. Like C, Add define definition in any place.
We usually it to header file(.h)
Example
#define kABC 123 #define kStr @"Const"
const
Const is C++ prefix. Objective-C also support
The way is a bit interesting.
Add extern in header and also, add
Example
Header
extern const int kABC; extern NSString * const kStr;
Code
const int kABC = 123; NSString * const kStr = @"Const";
How to use?
NSLog(@"Const %d", kABC); NSLog(@"Const %@", kStr);
The use is same.
What’s difference?
What is differences between two?
#define makes new variable(only replace var) every time this is shown up.
extern const is only one var.
You are a very clever individual!