If I declare a string constant like so:
You should create a header file like
// Constants.h
extern NSString * const MyFirstConstant;
extern NSString * const MySecondConstant;
//etc.
You can include this file in each file that uses the constants or in the pre-compiled header for the project.
You define these constants in a .m file like
// Constants.m
NSString * const MyFirstConstant = @"FirstConstant";
NSString * const MySecondConstant = @"SecondConstant";
What do I do to define integer constants?
Integer constants are interpreted as decimal values (base 10) by default. To specify a constant that is not in base 10, use the following extension syntax: [s] [[base] #] nnn... Is an optional plus (+) or minus (-) sign.
Character constants, real constants, and integer constants, etc., are types of primary constants.
1) An integer constant must have at least one digit. 2) It must not have a decimal point. 3) It can either be positive or negative. 4) No commas or blanks are allowed within an integer constant. 5) If no sign precedes an integer constant, it is assumed to be positive.
All integers are constants, but not all constants are integers. All integers are constants, but not all constants are integers.
Replace NSString* with NSInteger.
This is true of any constant type, be it a primitive such as int/float, or a class such as NSString or NSInteger.
The only thing to be aware of is whether you desire a constant or a pointer to a constant (such as withNSString), and how it's initialized in the .m file
Integer example:
// constants.h
extern NSInteger const MyIntegerConstant;
// constants.m
NSInteger const MyIntegerConstant = 666;
(Note: for the reason why NSInteger instead of just regular "int", see this post)
Class example:
// constants.h
extern MyClass* const MyClassConstant;
// constants.m
MyClass* const MyClassConstant= [[MyClass alloc] initWith: paramOne and:paramTwo];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With