Open In App

Literals in Objective-C

Last Updated : 15 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Literals are the values that are assigned to a variable and which remain constant over the whole program. Literals are the values that cannot be modified in the program after declaration. Literals values are taken the same as variable values but their values cannot be altered in a program during its execution. Literals contain some basic data types to create or store some constant data types having some values like integer literals, float literals, character literals, or string literals. we can use direct library methods or constant keywords or #define preprocessor to create literals. If we are creating the literals or constant values with the help of keywords in a programming language then the same concept will be followed as in other programming languages but if we are using library methods for storing constant values then it will have its own syntax to store it that is, a keyword for creating constant values after that declaration of the pointer in which we have to store the values and the values are stored only by writing the ‘@’ symbol precedent to the value.

Now let us understand the types of literals in objective-c one by one,

NSNumber Literals

An integer literal is used to store the integer values and these integer literals can be specified in two ways, i.e using a prefix that specifies the base of integer literal (0x for hexadecimal, 0 for octal, and nothing for decimal) or by using suffix which specifies the type of an integer literal (combination of U and L are used for specifying the literal as unsigned and long respectively). We can use the NSInteger keyword also to store only the integer values.

Some examples of integer literals –

53        /* integer value having decimal value as base */

33u       /* unsigned integer value having decimal value as base */

31ul      /* unsigned long integer value having decimal value as base */

33uu      /* unsigned unsigned integer value having decimal value as base */

0213      /* integer value having octal value as base */

NSNumber can also store float values also to store real numbers and integer values which values have a decimal part, a fractional part, and an exponential part while representing the decimal form of an integer you must include the decimal point, exponent, or both and while using the exponential form you must include the integer part, fractional part or both.

Some examples of floating values – 

3.14

123e-45

3.14e-7

Example:

ObjectiveC




#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
   NSNumber *num = [[NSNumber alloc] initWithInt : 47];
   NSLog (@"Number is : %@", num);
    
   [pool drain];
   return 0;
}


Output:

Output

Output

NSString Literals

String literals are the same as the character literals but in this, we can store multiple characters. string literals are enclosed in double quotes e.g. “hello”. storing the string like this means we cannot change this value anywhere in the program during execution.

Example of String literals-

NSString *var = @”hello”;

NSString Literals are also used to store single character values, an escape sequence, and some special meaning characters values that are enclosed in single quotes e.g. ‘a’ or ‘\n’. Char keyword is used as a data type to store the values. In character literals, we cannot store a number of characters because this will lead to a warning, for this purpose we have to create an array of characters to store multiple character values.

Some examples of character values-

‘a’    /* simple character literal */

‘\b’   /* uses for backspace */

‘\t’   /* uses for tabspace */

‘\n’   /* use for newline */

… and many more

Example:-

ObjectiveC




#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
   NSString *word = @"hello everyone";
   NSLog(@"constant value is : %@", word);
   [pool drain];
   return 0;
}


Output:-

Output

Output

NSArray Literals

Array literals are used to store the values of multiple elements in a single variable and make it a constant for the whole program so that the value of this cannot be changed during the execution of the program. In this NSArray keyword, we input any type of value but all the values have to be of the same type. There also exists one NSMutableArray, using this type of array we can change the values of the array anywhere in the program.

Example to create an array literal –

NSArray *name = @[@”john”, @”stan”, @”charles”, @”andrew”];

Example:-

ObjectiveC




#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
   NSArray *name = [[NSArray alloc] initWithObjects : @"john", @"andrew", @"stan", @"charles", nil];
   NSLog (@"%@", name);
    
   [pool drain];
   return 0;
}


Output:-

Output

Output

NSDictionary Literals

Dictionary Literals are created to store the values or elements like an array but there are two values stored one is the “key” and another is its “value”. This dictionary literal is used to store data having two values for one with index and creating this keyword as literal will remain the variable as constant means we cannot change its value anywhere during the program execution. There also exists one NSMutableDictionary, using this type of array we can change the values of dictionary-type variables anywhere in the program.

Example to create a dictionary literal –

NSDictionary *data = @[@”id” : @”123″, @”name” : @”john”, @”location” : @”texas”]

Example:

ObjectiveC




#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
   NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys :
                         @"123", @"Id",
                         @"Texas", @"Location",
                         @"Andrew", @"Name",
                         nil];
   NSLog (@"%@", dict);
    
   [pool drain];
   return 0;
}


Output:-

Output

Output

We can also use some other keywords to create literals like the “const” keyword and the “#define” keyword to create some constant values. Let us take an example to understand this:-

Program 1

In this program we have printed the constant integer value using the const keyword :

ObjectiveC




#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   const int num = 11;
   NSLog(@"constant value is : %d", num);
   [pool drain];
   return 0;
}


Output:

Output

Output – 1

Program 2

In this program, we have print the constant integer value using the #define keyword and this keyword is initialized before the main function:

ObjectiveC




#import <Foundation/Foundation.h>
 
#define word "hello"
 
int main (int argc, const char * argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
   NSLog(@"constant value is : %s", word);
    
   [pool drain];
   return 0;
}


Output:

Output

Output – 2



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads