Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

objective c
communitycreator

What is typedef in objective C?

Salman Yousaf

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

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.

Syntax


typedef unsigned char byte;

Example

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 methods
printf("%s\n", m);
printf("%s\n", mybird);
NSLog(@"Your value is %i", yourdollar);
NSLog(@"Your value is %llu", i);
[pool drain];
return 0;
}

Explanation

  • Line 1: We import the Foundation framework that loads the base structure for Objective C programming.
  • Lines 2–4: We define the data types with new names by using the 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.
  • Lines 7–10: Inside the main function, we give the values against the variable names. We use the new names of the data types given at the start.
  • Lines 11–14: We print the values of the variables 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.

Use 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;
}

Explanation

  • Line 3: We define the structure, Drinks, and adding typedef at the beginning. This will allow us to give a custom name to struct Drinks.
  • Lines 4-5: Inside the Structure, we define the variables Drink_name, Size, and drink_id with data types.
  • Line 6: We give an alias to the struct Drinks as Drink.
  • Line 8: We create the object of the structure as mydrink.
  • Lines 9–11: We assign values to the variables of the structure. For any string value, we use "string value".
  • Lines 12–14: We print the values of the variables by using the NSLog method along with the messages.

RELATED TAGS

objective c
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring