Dynamic Typing in Objective-C
Last Updated :
28 Apr, 2025
The term "dynamic typing" refers to a variable's type only being established during runtime. Strong typing means that when executing operations, types must be compatible with the operand. For instance, adding an integer to a floating-point number in Python results in an error, whereas adding an integer to a text does not.
When the type of the object a variable points to is not verified during compilation, the variable has a dynamic type. Without identifying the type of object it is, Objective-C utilizes the id data type to represent a variable that is an object. It is known as dynamic typing in this context.
Let's say, for instance, that we wish to create a function that receives an object as input. The object's displayInfo method is called within the function. We would need to create a separate function for each class type we wished to be able to handle if dynamic typing and dynamic binding didn't exist. We can declare the object argument as of type id thanks to dynamic typing and binding in Objective-C, which enables us to send any object through to the function. Second, we can rely on dynamic binding to make sure that we are using the proper object's displaying method when we do so within the function (i.e. the one that was passed through as an argument).
When designing object-oriented programming, it's common that we won't always be able to predict what kind of object will eventually need to be assigned to a variable. This is especially true when providing objects to functions or methods as parameters. Instead of creating a function or method specifically for each class in an application, it is considerably simpler to create a single, general-purpose function or method that can handle an object from any class. Here, the idea of dynamic typing is applied. Due to dynamic typing, we can declare a variable that can hold any kind of object, regardless of the class from which it derives. The Objective-C id type is used to accomplish this.
Example 1:
ObjectiveC
// Objective-C for dynamic typing
NSArray *Example_Array = [NSArray arrayWithObjects: @ "It's GFG String",
[NSDecimalNumber zero], [NSDate date], nil];
NSInteger ind;
// Running the for loop for ind
for (ind = 0; ind < 3; ind++)
{
id anObject = [Example_Array objectAtIndex : ind];
NSLog(@"Object at index %d is %@", ind, [anObject description]);
}
Example 2:
ObjectiveC
// Objective-C for dynamic typing
// Starting of main function
int main (int argum, const char * ar[])
{
@autoreleasepool
{
// Object created
id obj_1;
// allocating value to object
obj_1 = [[account alloc] init];
[obj_1 set_ac: 976456 And_Bal: 30];
// Displaying the created object
objDisplay (obj_1);
obj_1 = [[Cust_Info alloc] init];
objDisplay (obj_1);
}
return 0;
}
Static vs Dynamic Typing
Following are the differences between static and dynamic typing:
- Type checking is done at runtime for dynamically typed languages and at compile time for statically typed languages. Thus, scripts written in dynamically typed languages, such as Groovy, can compile even if they have faults that will prohibit the script from functioning properly (if at all).
- The second difference is that dynamically typed languages do not require you to declare the data types of your variables prior to using them, whereas statically typed languages do.
- Static typing, in contrast, clearly identifies the class to which an object belongs at build time. Dynamic typing does not do this. Dynamic typing allows your software significantly greater freedom in exchange for the stricter data integrity that static type checking at build time may provide. Object introspection also allows you to verify an object's type at runtime and confirm its suitability for a given operation. For instance, you could ask an anonymous, dynamically typed object what class it belongs to.
Similar Reads
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
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
Dynamic Binding in Objective-C As the name suggests Dynamic binding is the process of joining two different things or creating a relation between different things. Or we can say that dynamic binding is a process of determining a method to invoke at runtime, not at the compiler time. Objective - C is a programming language that su
5 min read
Strings in Objective-C Strings are a fundamental concept in programming, and they are used to represent text in many different applications. In Objective-C, a string is an object that represents a sequence of characters. A string can be created using several built-in classes and methods, which allow you to manipulate the
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
Typedef in Objective C A typedef allows us to define a data type with a new name, for example, typedef unsigned char CAT. Now from now, onwards CAT can be used as an abbreviation for unsigned char, for example, CAT num1, num2;. Or in other words, we can say that typedef is a keyword that is used to assign the new name to
3 min read