articles

Home / DeveloperSection / Articles / Objective-C : Typedef

Objective-C : Typedef

Tarun Kumar3849 14-Jul-2015

Previously we seen how the NSLog use in ObjC  : Objective-C Log Handling

 

The Objective-C programming language provides a keyword called typedef, which you can use to give a type a new name. You can use typedef to give a name to user-defined data type as well. For example, you can use typedef with enum to define a new data type and then use that data type to define enum variables directly as follows:

typedef enum {
    kCircle,
    kRectangle,
    kOblateSpheroid
} ShapeType; 


Three things are being declared here: an anonymous enumerated type is declared, ShapeType is being declared a typedef for that anonymous enumeration, and the three names kCircle, kRectangle, and kOblateSpheroid are being declared as integral constants.

 

Let's break that down. In the simplest case, an enumeration can be declared as

enum tagname {
       ...
}; 

This declares an enumeration with the tag tagname. In C and Objective-C (but not C++), any references to this must be preceded with the enum keyword. For example:

enum tagname x;  // declare x of type 'enum tagname'
tagname x;  // ERROR in C/Objective-C, OK in C++

 

In order to avoid having to use the enum keyword everywhere, a typedef can be created:

enum tagname { ... };
typedef enum tagname tagname;  // declare 'tagname' as a typedef for'enum tagname'

 

This can be simplified into one line:
typedef enum tagname { ... } tagname; // declare both 'enum tagname' and 'tagname'

 

And finally, if we don't need to be able to use enum tagname with the enum keyword, we can make the enum anonymous and only declare it with the typedef name:

typedef enum { ... } tagname; 


Now, in this case, we're declaring ShapeType to be a typedef'ed name of an anonymous enumeration. ShapeType is really just an integral type, and should only be used to declare variables which hold one of the values listed in the declaration (that is, one of kCircle, kRectangle, and kOblateSpheroid). You can assign a ShapeType variable another value by casting, though, so you have to be careful when reading enum values.

Finally, kCircle, kRectangle, and kOblateSpheroid are declared as integral constants in the global namespace. Since no specific values were specified, they get assigned to consecutive integers starting with 0, so kCircle is 0, kRectangle is 1, and kOblateSpheroid is 2.

 

Following is another example to define a term BYTE for one-byte numbers:

typedef unsigned char BYTE;

 

After this type definition, the identifier BYTE can be used as an abbreviation for the type unsigned char, for example:

BYTE  b1, b2;

 

By convention, uppercase letters are used for these definitions to remind the user that the type name is really a symbolic abbreviation, but you can use lowercase, as follows:

typedef unsigned char byte;

 

You can use typedef to give a name to user-defined data type as well. For example, you can use typedef with structure to define a new data type and then use that data type to define structure variables directly as follows:

#import <Foundation/Foundation.h>
typedef struct Books
{
   NSString *title;
   NSString *author;
   NSString *subject;
   int book_id;
} Book;
 
int main( )
{
   Book book;
   book.title = @"Objective-C Programming";
   book.author = @"TutorialsPoint";
   book.subject = @"Programming tutorial";
   book.book_id = 100;
   NSLog( @"Book title : %@\n", book.title);
   NSLog( @"Book author : %@\n", book.author);
   NSLog( @"Book subject : %@\n", book.subject);
   NSLog( @"Book Id : %d\n", book.book_id);

   return 0;
}

After compiling the source Code:

2015-07-14 05:17:18.305 demo[23272] Book title : Objective-C Programming 2015-07-14 05:17:18.306 demo[23272] Book author : TutorialsPoint 2015-07-14 05:17:18.306 demo[23272] Book subject : Programming tutorial 2015-07-14 05:17:18.306 demo[23272] Book Id : 100

Another Example:

#import <Foundation/Foundation.h>

typedef unsigned char ColorComponent;

int main(int argc, const char * argv[])
{
    ColorComponent red = 255;
    ColorComponent green = 160;
    ColorComponent blue = 0;
    NSLog(@"Your paint job is (R: %hhu, G: %hhu, B: %hhu)",
          red, green, blue);
    return 0;
}

After compiling the source Code:

2015-07-14 05:57:23.571 demo[17778] Your paint job is (R: 255, G: 160, B: 0)

 

The #define is a Objective-C directive, which is also used to define the aliases for various data types similar to typedef but with following differences:

-  The typedef is limited to giving symbolic names to types only whereas #define can be used to define alias for values as well, like you can define 1 as ONE, etc.

-  The typedef interpretation is performed by the compiler where as #define statements are processed by the pre-processor.


Following is a simplest usage of #define:


#import <Foundation/Foundation.h>
#define TRUE  1
#define FALSE 0
int main( )
{
   NSLog( @"Value of TRUE : %d\n", TRUE);
   NSLog( @"Value of FALSE : %d\n", FALSE);
   return 0;
}


After compiling the source Code:


2015-07-14 05:24:12.579 demo[27646] Value of TRUE : 1 2015-07-14 05:24:12.580 demo[27646] Value of FALSE : 0

 

Next, we will learn about:Objective-C : Macros


Updated 08-Aug-2019

Leave Comment

Comments

Liked By