#define
December 13, 2013 -When to use #define:
-
When you’re declaring macros like in Apple’s:
#define UI_USER_INTERFACE_IDIOM() ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? [[UIDevice currentDevice] userInterfaceIdiom] : UIUserInterfaceIdiomPhone)
I’m actually not even sure why this wasn’t made as a function like
NSLog instead of a macro.
When not to use #define:
-
When you’re declaring constants. Constants should look like:
NSString *const RWSSomeConstantName = @"theValueDoesn'tUsuallyMatter"; const NSInteger RWSIntegerConstant = 3;
Why go through this effort to type extra letters?
- Constants declared using
#definedon’t show up in tools like the debugger.#defineis raw text substitution, constants are symbols to your debugger, like class names. #definewon’t warn you if you use#definein some other file and change the value of your constant accidentally.