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:

 

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:

 

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:

 

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:

 

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 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.

Similar Reads

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:...

Conclusion

...

Contact Us