How to use a constant in Objective-C

Objective-C is an object-oriented programming language used for the development of MacOS and IOS operating systems. Brad Cox and Tom Love created Objective-C in the early 1980s. They built Objective-C on C programming language. Objective-C supports classes and objects while C does not. C code will work in Objective-C but Objective-C code won't work in C.

This reference is for beginners to help them understand constants and their usage in Objective-C programming language. Prior knowledge of C language will aid in quick understanding but it is not a pre-requisite.

Constants in Objective-C

A constant refers to a fixed value of data that cannot be altered or modified during program execution. Once a programmer defines a constant, it can be reused many times during program execution. We can define a constant in Objective-C using the preprocessor directive #define followed by the identifier in uppercase, and a value. The preprocessor directive is a keyword that begins with # and tells the compiler what to do during the compilation of the source file. The value of a constant can be any of the Objective-C data types: int, float, double, char or object.

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// constant
#define PI 3.142
float area;
int r = 2;
area = PI * (r * r);
NSLog(@"Area is %f", area);
[pool drain];
return 0;
}

Code explanation

  • Line 1: We include the Foundation frameworkObjective-C Foundation framework is a set of classes provided to ease and speed the process of developing applications in Objective-C. It is similar to the standard library in C. All classes in the Foundation framework begin with NS. We'll include the Foundation framework in a program using the the preprocessor directive #import . Also, we can selectively include header files for only the classes needed in the program like using #import . header files.

  • Line 5: We initialize NSAutoreleasePool. It is used to manage the lifecycle of an object. It keeps track of all the object constants allocated by the system so they can be released properly.

  • Line 7: We declare PI with a value of 3.142 using the #define preprocessor directive.

  • Line 8: We declare a variable area.

  • Line 9: We declare a variable r and assign it a value of 2.

  • Line 10: We compute PI*(r*r) and assign the result to area.

  • Line 11: We log the value of area using NSLog.

  • Line 12: We release the pool.

We can also define a constant using the keyword const followed by the data type, identifier in uppercase, and the value. Let's see an example of defining a constant using const keyword in Objective-C.

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// constant
const float pi = 3.142;
float area;
int r = 2;
area = pi * (r * r);
NSLog(@"Area is %f", area);
[pool drain];
return 0;
}

Here, we declare pi with the const keyword and assign it a value of 3.142 on line 8.

Conclusion

This Answer focuses on the usage of a constant in Objective-C. A constant is a fixed value of data that cannot be altered or modified during program execution. We can define a constant in Objective-C in one of the following ways:

  • Using #define identifier: #define IDENTIFIER value

  • Using const keyword: const datatype identifier = value

Free Resources