Reputation: 2500
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?
Upvotes: 15
Views: 14793
Reputation: 58786
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];
Upvotes: 39