Open In App

Constants in Objective-C

Last Updated : 22 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Constants in Objective-C are values that cannot be modified once they are set. They are used to store a variety of data types, including numbers, strings, and booleans. Constants are a fundamental feature of the Objective-C programming language and are widely used in the development of applications for Apple’s macOS and iOS platforms. They are particularly useful for storing values that will be used frequently throughout a program, such as the maximum limit of a loop or the value of a mathematical constant. Using constants helps to reduce errors and make the code more readable by allowing developers to assign a meaningful name to a value, rather than having to remember the value itself.

Constants can also be declared as global or static, which means that they have a single shared value throughout the program. This can be useful for storing values that will be used by multiple functions or methods while keeping the program’s memory usage low. Constants in Objective-C are a powerful tool for creating reliable and maintainable code.

To declare a constant in Objective-C, you use the keyword const. Constants can be declared at the beginning of a function or method, or at the global level. 

Syntax:

const int MAX_COUNT = 100;

Here, MAX_COUNT is the name of the constant and 100 is the value that it is set to. The keyword int specifies the data type of the constant.

Types of Constants

There are several different types of constants in Objective-C, each with its own unique characteristics and uses. The most common types of constants include:

Numeric Constants

These are constants that represent numerical values, such as integers, floating-point numbers, and complex numbers. Numeric constants can be written using standard decimal, octal, or hexadecimal notation. For example, the decimal constant 42, the octal constant 052, and the hexadecimal constant 0x2A are all equivalent.

Example:

ObjectiveC




// Objective-C program to illustrate numeric constant
#import <Foundation/Foundation.h>
  
int main(int argc, const char * argv[]) 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
      
    // Numeric constant
    const int MAX_HEIGHT = 100;
    NSLog(@"Max Height: %d", MAX_HEIGHT);
    [pool drain];
    return 0;
}


Output:

Max Height: 100

String Constants

These are constants that represent a sequence of characters, such as words or phrases. String constants can be written using double quotes (“) or single quotes (‘) and can contain any combination of letters, numbers, and special characters. For example, the string constant “Hello, World!” is a string constant.

Example:

ObjectiveC




// Objective-C program to illustrate string constant
#import <Foundation/Foundation.h>
  
int main(int argc, const char * argv[]) 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
      
    // String constant
    NSString * const GREETING = @"Hello, World!";
    NSLog(@"%@", GREETING);
    [pool drain];
    return 0;
}


Output:

Hello, World!

Boolean Constants

These are constants that represent a true or false value. Boolean constants can be written using the keywords YES or NO, or the keywords TRUE or FALSE. For example, the boolean constant YES is a boolean constant.

Example:

ObjectiveC




// Objective-C program to illustrate boolean constant
#import <Foundation/Foundation.h>
  
int main(int argc, const char * argv[]) 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
      
    // Boolean constant
    const BOOL IS_VALID = YES;
    NSLog(@"Is Valid: %d", IS_VALID);
      
    [pool drain];
    return 0;
}


Output:

Is Valid: 1

Object Constants

These are constants that represent an instance of an object. Object constants can be created using the alloc and init methods, and can be used to store a reference to an object.

Example:

ObjectiveC




// Objective-C program to illustrate object constant
#import <Foundation/Foundation.h>
  
int main(int argc, const char * argv[]) 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
      
    // Creating object constant
    NSString * const PI = [[NSString alloc] initWithFormat:@"%.2f", M_PI];
    NSLog(@"Value of PI: %@", PI);
      
    [pool drain];
    return 0;
}


Output:

Value of PI: 3.14



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads