Open In App

Type Casting in Objective-C

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

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 useful in a wide range of programming scenarios. This article will provide an in-depth overview of type casting in Objective-C, including examples of all types and subtypes.

What is Type Casting?

Type casting in Objective-C is the process of converting one data type to another. Objective-C has several types and subtypes, including:

  1. Implicit type conversion: The compiler performs this type of type casting automatically when a value is assigned to a variable of a different type.
  2. Explicit type conversion: This is done by the programmer and necessitates the use of specific syntax and keywords.
  3. Narrowing type conversion: Narrowing type conversion involves converting a larger data type to a smaller one, which can result in data loss.
  4. Widening type conversion: In this type of type casting, a smaller data type is converted to a larger one without data loss.

Syntax and Related Keywords

In Objective-C, type casting is performed by using the appropriate keyword for the conversion. In Objective-C, the most commonly used keywords for typecasting are:

  1. (type) variableName: This syntax is used for explicit type conversion, where ‘type’ is the data type to which the variable is being converted and’variableName’ is the variable’s name.
  2. intValue: This keyword is used to convert a string or float to an integer.
  3. floatValue: This keyword transforms an integer or string into a floating-point number.
  4. bool value: This keyword transforms an object into a Boolean value.

Here are a few examples of how typecasting can be used in Objective-C:

Example 1: Implicit type conversion

ObjectiveC




#import <Foundation/Foundation.h>
  
int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
        
      // Implicit type conversion example
    int i = 10;
    float f = i;
    NSLog(@"The value of i is %d", i);
    NSLog(@"The value of f is %f", f);
   
    [pool drain];
    return 0;
  
}


Explanation: The integer variable i is assigned to the float variable ‘f’ in this example. This is an example of implicit type conversion, in which the compiler converts the value to the appropriate data type automatically.

Output:

output

 

Example 2: Explicit type conversion

ObjectiveC




#import <Foundation/Foundation.h>
  
int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
     // Explicit type conversion example
    float f = 10.5;
    int i = (int)f;
    
    NSLog(@"The value of i is %d", i);
    NSLog(@"The value of f is %f", f);
   
    [pool drain];
    return 0;
  
}


Explanation: The float variable ‘f’ is explicitly converted to an integer in this example by using the (int) keyword. The value obtained is assigned to the integer variable ‘i’.

Output:

output

 

Example 3: Narrowing type conversion

ObjectiveC




#import <Foundation/Foundation.h>
  
int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
     // Narrowing type conversion example
    double d = 3.14159;
    float f = (float)d;
    NSLog(@"The value of d is %f", d);
    NSLog(@"The value of f is %f", f);
  
    [pool drain];
    return 0;
  
}


Explanation: In this example, the (float) keyword is used to explicitly convert the double variable ‘d’ to a float. As the data type is being converted from a larger to a smaller size, this is an example of narrowing type conversion.

Output:

output

 

Example 4: Widening type conversion

ObjectiveC




#import <Foundation/Foundation.h>
  
int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
     // Widening type conversion example
    int i = 10;
    double d = i;
    NSLog(@"The value of i is %d", i);
    NSLog(@"The value of d is %f", d);
  
    [pool drain];
    return 0;
  
}


Explanation: In this example, the integer variable ‘i’ is assigned to the double variable ‘d’. This is an example of widening type conversion, as the data type is converted from a smaller size to a larger one. When the application is run, the following results will be produced:

Output:

output

 

Conclusion

To summarise, type casting is a crucial concept in Objective-C that allows us to convert data from one data type to another. In Objective C, there are two types of type conversions: implicit and explicit. Implicit type conversion is performed automatically by the compiler when values of one data type are assigned to variables of another data type that are wider. Explicit type conversion, on the other hand, necessitates the creation of explicit code to carry out the conversion.

Explicit type conversion in Objective-C is further subdivided into narrowing and widening conversions, depending on whether the data type is being converted from a wider to a narrower data type or from a narrower to a wider data type.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads