Open In App

Dynamic Typing in Objective-C

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

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.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads