Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constants in Objective-C

I'm developing a Cocoa application, and I'm using constant NSStrings as ways to store key names for my preferences.

I understand this is a good idea because it allows easy changing of keys if necessary.
Plus, it's the whole 'separate your data from your logic' notion.

Anyway, is there a good way to make these constants defined once for the whole application?

I'm sure that there's an easy and intelligent way, but right now my classes just redefine the ones they use.

like image 762
Allyn Avatar asked Feb 11 '09 21:02

Allyn


People also ask

What is const in Objective-C?

Advertisements. The constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal.

What does #define do in Objective-C?

#define name value , however, is a preprocessor command that replaces all instances of the name with value . For instance, if you #define defTest 5 , all instances of defTest in your code will be replaced with 5 when you compile.

How do you declare a variable in Objective-C?

Variable Declaration in Objective-C You will use extern keyword to declare a variable at any place. Though you can declare a variable multiple times in your Objective-C program but it can be defined only once in a file, a function or a block of code.

What is extern Objective-C?

Keyword extern is used for declaring extern variables in Objective-C. This modifier is used with all data types like int, float, double, array, pointer, function etc. Basically extern keyword extends the visibility of the C variables and C functions. Probably that's is the reason why it was named as extern.


2 Answers

You should create a header file like:

// Constants.h FOUNDATION_EXPORT NSString *const MyFirstConstant; FOUNDATION_EXPORT NSString *const MySecondConstant; //etc. 

(You can use extern instead of FOUNDATION_EXPORT if your code will not be used in mixed C/C++ environments or on other platforms.)

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"; 

Constants.m should be added to your application/framework's target so that it is linked in to the final product.

The advantage of using string constants instead of #define'd constants is that you can test for equality using pointer comparison (stringInstance == MyFirstConstant) which is much faster than string comparison ([stringInstance isEqualToString:MyFirstConstant]) (and easier to read, IMO).

like image 153
Barry Wark Avatar answered Sep 20 '22 18:09

Barry Wark


Easiest way:

// Prefs.h #define PREFS_MY_CONSTANT @"prefs_my_constant" 

Better way:

// Prefs.h extern NSString * const PREFS_MY_CONSTANT;  // Prefs.m NSString * const PREFS_MY_CONSTANT = @"prefs_my_constant"; 

One benefit of the second is that changing the value of a constant does not cause a rebuild of your entire program.

like image 44
Andrew Grant Avatar answered Sep 20 '22 18:09

Andrew Grant