Composite Objects in Objective-C
Last Updated :
24 Apr, 2025
In Objective-C, composite objects are objects that are composed of other objects. This means that an object can have one or more objects as its instance variables, and it can also be a part of another object’s instance variables.
Composite objects are useful in many scenarios where complex data structures are required. For instance, a complex user interface that has multiple components can be represented using composite objects. Similarly, a game that has multiple levels, enemies, and weapons can also use composite objects.
Types of Composite Objects
NSArray is a class in Objective-C that represents an ordered collection of objects. It is used to store and retrieve a list of objects of the same type. The objects are stored in a linear sequence, and each object is assigned an index number starting from zero.
Syntax:
NSArray *array = [[NSArray alloc] initWithObjects: object1, object2, object3, …, nil];
‘NSArray’ is the class name.
‘array’ is the name of the instance of the class.
‘alloc’ is a class method that allocates memory for the array.
‘ initWithObjects’ is an instance method that initializes the array with the specified objects.
‘object1’, ‘object2‘, ‘object3’, … are the objects to be stored in the array.
‘nil’ is a sentinel value that marks the end of the list of objects.
Example:
ObjectiveC
#import <Foundation/Foundation.h>
int main( int argc, const char * argv[]) {
@autoreleasepool {
NSArray *vegetables = @[ @"Brinjal" , @"Onion" , @"Cabbage" , @"Potato" ];
for ( NSString *vegetable in vegetables) {
NSLog ( @"%@" , vegetable);
}
}
return 0;
}
|
Approach: In this example, we create an NSArray object called array that contains four string objects: “Brinjal”, “Onion”, “Cabbage” and “Potato”. And print the results using NSLog().
Output:
NSDictionary is a class in Objective-C that is used to store collections of key-value pairs, also known as an associative array or a map. The keys are used to uniquely identify each value, and they can be any object that conforms to the ‘NSCopying’ protocol. It provides a number of methods to manipulate and retrieve data from the collection.
Syntax:
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
value1, key1,
value2, key2,
value3, key3,
nil];
In the above syntax, ‘initWithObjectsAndKeys’ is the initializer method that creates an instance of NSDictionary. It takes a variable number of arguments, where each key-value pair is represented by two consecutive arguments.
Example:
ObjectiveC
#import <Foundation/Foundation.h>
int main( int argc, const char * argv[]) {
@autoreleasepool {
NSDictionary *dictionary = [[ NSDictionary alloc] initWithObjectsAndKeys:
@"XYZ" , @"firstName" ,
@"abc" , @"lastName" ,
@"30" , @"age" ,
nil ];
NSString *firstName = [dictionary objectForKey: @"firstName" ];
NSString *lastName = [dictionary objectForKey: @"lastName" ];
NSNumber *age = [dictionary objectForKey: @"age" ];
NSLog ( @"First name: %@" , firstName);
NSLog ( @"Last name: %@" , lastName);
NSLog ( @"Age: %@" , age);
[dictionary setValue: @"New York" forKey: @"city" ];
NSLog ( @"Dictionary: %@" , dictionary);
}
return 0;
}
|
Approach: In this example, we create an instance of NSDictionary with three key-value pairs: “firstName”-“XYZ”, “lastName”-“abc”, and “age”-“30”. We then access these values using the objectForKey: method and print them to the console using NSLog.
We also add a new key-value pair to the dictionary using the setValue:forKey: method and print the updated dictionary using NSLog.
Output:
NSSet is a collection class that stores an unordered set of unique objects. It is similar to an array, but unlike arrays, sets do not store objects in a specific order, and they can only contain unique objects. To create an NSSet, you can use the setWithObjects: method, which takes a variable number of arguments. One real-life example of NSSet in Objective-C is in a music streaming application.
Syntax:
NSSet *mySet = [NSSet setWithObjects:obj1, obj2, obj3, …, nil];
This creates an NSSet object mySet containing the objects ‘obj1’, ‘obj2‘, ‘obj3‘, and so on. The last argument to the method is nil, indicating the end of the argument list.
ObjectiveC
#import <Foundation/Foundation.h>
int main( int argc, const char * argv[]) {
@autoreleasepool {
NSSet *stringSet = [ NSSet setWithObjects: @"Apple" , @"Banana" , @"Cherry" , nil ];
for ( NSString *string in stringSet) {
NSLog ( @"%@" , string);
}
BOOL containsApple = [stringSet containsObject: @"Apple" ];
if (containsApple) {
NSLog ( @"The set contains Apple" );
} else {
NSLog ( @"The set does not contain Apple" );
}
}
return 0;
}
|
Approach: In this example, we first create a set of NSString objects using the setWithObjects: method. We then loop through the set using a for-in loop and print each string to the console using NSLog. Finally, we check if the set contains the string “Apple” using the containsObject: method and print a message to the console depending on the result.
Output:

Similar Reads
Classes & Objects in Objective-C
Objective-C is an object-oriented programming language that has been used for developing software applications for various Apple platforms, such as iOS, macOS, watchOS, and tvOS. Classes and objects are the fundamental building blocks of object-oriented programming in Objective-C. A class is a bluep
8 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
Type Casting in Objective-C
Objective-C is a programming language that was created in the 1980s and is widely used for developing software for the macOS and iOS platforms. One of Objective-C's key features is its ability to perform typecasting. Type casting enables programmers to convert one data type to another, which is usef
4 min read
Operators in Objective-C
The Objective-C language is developed on top of C Programming. The operator in Objective-C is the same as the C language operators. It is primarily used in developing iOS and MacOS operating systems and software applications for iOS. Operators are used to forming a mathematical expression using vari
11 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
Properties in Objective-C
The object-oriented programming language Objective-C is largely used to create applications for Apple's macOS and iOS platforms. It incorporates all of the characteristics of C while also providing more features for object-oriented programming, making it a superset of the C programming language. The
5 min read
Data Types in Objective-C
In Objective-C, each variable has an associated data type and each data type has different amounts of memory required in a system. A data type is a classification of data that tells the compiler or interpreter how the programmer intends to use the data. Or in other words, a data type represents what
8 min read
Singleton Class in Objective-C
Objective-C serves as a programming language widely employed to construct applications intended for both macOS and iOS. Among the many design patterns that find extensive usage in Objective-C, the Singleton pattern proves quite noteworthy. A Singleton represents a class in that instantiation remains
6 min read
Posing in Objective-C
In Objective-C, posing is a technique that allows a class to wholly replace another class within a program. The replacing class is said to "pose as" the target class. All messages sent to the target class are instead received by the posing class. This article focuses on discussing posing. What is Po
5 min read
Constructors in Objective-C
Constructor is basically a function or method that is called automatically at the time of object creation of a class. constructor method has the same name as that of the class. It is basically used to initialize the data members of the new objects generally. It constructs or creates a new value for
8 min read