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:
OutputNSString 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:-
OutputNSArray 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:-
OutputNSDictionary 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
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 - 1Program 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 - 2
Similar Reads
Interface in Objective-C Objective-C is an object-oriented programming language that was developed by Apple Inc. for its operating systems. One of the fundamental concepts of object-oriented programming is the ability to define interfaces, which specify the methods and properties that an object must have in order to be cons
7 min read
Numbers in Objective-C Normally we work with numbers in almost every programming language. But in all other programming languages when we work with numbers we use primitive data types like int, short, long, float, etc. in Objective - C programming numbers have a very wide range we store the basic data types always in the
6 min read
Inheritance in Objective-C In general, Inheritance is a mechanism by which a subordinate or child class acquires the traits and qualities of a superordinate class or other derived classes. It also allows extra features like taking child class properties and using them in other derived classes. The syntax of classes is used to
4 min read
Nested loops in Objective-C Like most programming languages, Objective-C also gives three basic repetition control statements, namely loops, which help programmers to execute a single or block of statements repetitively till the given condition is satisfied. Furthermore, these loops can be used one inside another which is call
10 min read
for loop in Objective-C Like most programming languages, Objective-C also has a repeating statement called for loop. It is a repetition control statement that allows you to repeatedly execute a single instruction or a block of instructions into a specified number of times. unlike other loops like while-loop or do-while loo
9 min read
Functions in Objective-C Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. It is the main programming language used by Apple for the OS X and iOS operating systems and their respective application programming interfaces (APIs), Cocoa and
8 min read
Log Handling in Objective-C Objective-C is a programming language commonly used for developing applications on Apple's macOS, iOS, and iPadOS platforms. One important aspect of programming in Objective-C is handling logs. Logging is the process of recording information about the state of a program or system in a log file, whic
6 min read
Constants in Objective-C 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
4 min read
File Handling in Objective-C File handling is an essential part of any programming language, and Objective C is no exception. It allows developers to create, read, update, and delete files on the file system. This can be useful for a wide range of applications such as storing user data, saving game progress, or logging applicat
9 min read
Categories in Objective-C Categories are an important concept in the Objective-C programming language. They allow developers to extend the functionality of existing classes without having to modify their original code. This article will discuss categories in Objective-C, their uses, and provide examples of their implementati
3 min read