Objective-C Typedef

             How Use Typedef in Objective-C

            The Objective-C programming dialect gives a catchphrase called typedef, which you can use to give a sort another name. Taking after is an illustration to characterize a term BYTE for one-byte numbers:

typedef unsigned char BYTE;

After this sort definition, the identifier BYTE can be utilized as a truncation for the sort unsigned roast, for instance:.

BYTE  b1, b2;

By tradition, uppercase letters are utilized for these definitions to remind the client that the sort name is truly a typical shortening, however you can utilize lowercase, as takes after:

typedef unsigned char byte;

You can utilize typedef to give a name to client characterized information sort too. For instance, you can utilize typedef with structure to characterize another information sort and after that utilization that information sort to characterize structure variables straightforwardly as takes after:

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

At the point when the above code is gathered and executed, it delivers the accompanying result:

2013-09-12 12:21:53.745 demo[31183] Book title : Objective-C Programming
2013-09-12 12:21:53.745 demo[31183] Book author : TutorialsPoint
2013-09-12 12:21:53.745 demo[31183] Book subject : Programming tutorial
2013-09-12 12:21:53.745 demo[31183] Book Id : 100

typedef vs #define

The #define is an Objective-C mandate, which is additionally used to characterize the monikers for different information sorts like typedef however with taking after contrasts:
  • 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.
Taking after is an easiest use 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;
}

At the point when the above code is gathered and executed, it delivers the accompanying result:

2013-09-12 12:23:37.993 demo[5160] Value of TRUE : 1
2013-09-12 12:23:37.994 demo[5160] Value of FALSE : 0

0 comments:

Post a Comment

My Instagram