We can use Objective C to rename a datatype using the typedef
fucnction.
The typedef
function is used to assign the new name to the datatype. So that you can use a custom name for predefined long names of the datatypes. This will not only save time but also aids in having more understandable and easy-to-read code.
typedef unsigned char byte;
We declare three data types with the addition of typedef
in the starting.
#import <Foundation/Foundation.h>typedef char *String;typedef int Dollars;typedef unsigned long long ITS64;int main() {NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];String mybird = "Sparrow";String m = "hifza";Dollars yourdollar = 1000;ITS64 i = 18;// For Printing the values you can use NS Log and printf methodsprintf("%s\n", m);printf("%s\n", mybird);NSLog(@"Your value is %i", yourdollar);NSLog(@"Your value is %llu", i);[pool drain];return 0;}
typedef
keyword. For int
, we use Dollars and for unsigned long long, we use ITS64
.
This helps us from using such long names of data types again and again.m
, mybird
, yourdollar
, and i
. We use NSLog
to display a message along with the value of the variable.Note: This function also allows the programmers to assign new names to the user-defined data types. For Instance, you can consider renaming a structure Drinks.
typedef
with struct
This allows us to use Drink
only, instead of using struct Drinks
again and again.
#import <Foundation/Foundation.h>typedef struct Drinks{NSString *Drink_name;NSString *Size;int drink_id;} Drink;int main() {NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];Drink mydrink;mydrink.Drink_name = @"Strawberry Icecream Shake";mydrink.Size = @"Medium";mydrink.drink_id = 11;NSLog( @"Drink Name : %@\n", mydrink.Drink_name);NSLog( @"Drink Size : %@\n", mydrink.Size);NSLog( @"Drink # : %d\n", mydrink.drink_id);[pool drain];return 0;}
Drinks
, and adding typedef
at the beginning. This will allow us to give a custom name to struct Drinks
.Drink_name
, Size
, and drink_id
with data types.struct Drinks
as Drink
.mydrink
."string value"
.NSLog
method along with the messages.