Objective-C Structures

 How use structures in Objective-C

Objective-C clusters permit you to characterize kind of variables that can hold a few information things of the same kind yet structure is another client characterized information sort accessible in Objective-C programming which permits you to consolidate information things of various types.

Structures are utilized to speak to a record, Suppose you need to stay informed concerning your books in a library. You may need to track the accompanying properties about every book:

  • Title
  • Author
  • Subject
  • Book ID

Characterizing Structure

To characterize a structure, you must utilize the struct proclamation. The struct articulation characterizes another information sort, with more than one part for your project. The organization of the struct explanation is this:

struct [structure tag]
{
   member definition;
   member definition;
   ...
   member definition;
} [one or more structure variables];  

The structure tag is discretionary and every part definition is a typical variable definition, for example, int i; or buoy f; or some other substantial variable definition. Toward the end of the structure's definition, before the last semicolon, you can determine one or more structure variables yet it is discretionary. Here is the way you would pronounce the Book structure:

struct Books
{
   NSString *title;
   NSString *author;
   NSString *subject;
   int   book_id;
} book;  

Accessing Structure Members

To get to any individual from a structure, we utilize the part get to administrator (.). The part get to administrator is coded as a period between the structure variable name and the structure part that we wish to get to. You would utilize struct essential word to characterize variables of structure sort. Taking after is the sample to clarify utilization of structure:

#import <Foundation/Foundation.h>;

struct Books
{
NSString *title;
NSString *author;
NSString *subject;
int book_id;
};

int main( )
{
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
Book1.title = @"Objective-C Programming";
Book1.author = @"Nuha Ali";
Book1.subject = @"Objective-C Programming Tutorial";
Book1.book_id = 6495407;

/* book 2 specification */
Book2.title = @"Telecom Billing";
Book2.author = @"Zara Ali";
Book2.subject = @"Telecom Billing Tutorial";
Book2.book_id = 6495700;

/* print Book1 info */
NSLog(@"Book 1 title : %@\n", Book1.title);
NSLog(@"Book 1 author : %@\n", Book1.author);
NSLog(@"Book 1 subject : %@\n", Book1.subject);
NSLog(@"Book 1 book_id : %d\n", Book1.book_id);

/* print Book2 info */
NSLog(@"Book 2 title : %@\n", Book2.title);
NSLog(@"Book 2 author : %@\n", Book2.author);
NSLog(@"Book 2 subject : %@\n", Book2.subject);
NSLog(@"Book 2 book_id : %d\n", Book2.book_id);

return 0
}


At the point when the above code is ordered and executed, it creates the accompanying result:

2013-09-14 04:20:07.947 demo[20591] Book 1 title : Objective-C Programming
2013-09-14 04:20:07.947 demo[20591] Book 1 author : Nuha Ali
2013-09-14 04:20:07.947 demo[20591] Book 1 subject : Objective-C Programming Tutorial
2013-09-14 04:20:07.947 demo[20591] Book 1 book_id : 6495407
2013-09-14 04:20:07.947 demo[20591] Book 2 title : Telecom Billing
2013-09-14 04:20:07.947 demo[20591] Book 2 author : Zara Ali
2013-09-14 04:20:07.947 demo[20591] Book 2 subject : Telecom Billing Tutorial
2013-09-14 04:20:07.947 demo[20591] Book 2 book_id : 6495700

Structures as Function Arguments

You can pass a structure as a capacity contention in fundamentally the same path as you pass whatever other variable or pointer. You would get to structure variables in the comparable path as you have gotten to in the above case:

#import <Foundation/Foundation.h>;

struct Books
{
NSString *title;
NSString *author;
NSString *subject;
int book_id;
};

@interface SampleClass:NSObject

/* function declaration */
- (void) printBook:( struct Books) book ;

@end

@implementation SampleClass

- (void) printBook:( struct Books) book
{
NSLog(@"Book title : %@\n", book.title);
NSLog(@"Book author : %@\n", book.author);
NSLog(@"Book subject : %@\n", book.subject);
NSLog(@"Book book_id : %d\n", book.book_id);
}
@end

int main( )
{
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */

/* book 1 specification */
Book1.title = @"Objective-C Programming";
Book1.author = @"Nuha Ali";
Book1.subject = @"Objective-C Programming Tutorial";
Book1.book_id = 6495407;

/* book 2 specification */
Book2.title = @"Telecom Billing";
Book2.author = @"Zara Ali";
Book2.subject = @"Telecom Billing Tutorial";
Book2.book_id = 6495700;

SampleClass *sampleClass = [[SampleClass alloc]init];
/* print Book1 info */
[sampleClass printBook: Book1];

/* Print Book2 info */
[sampleClass printBook: Book2];

return 0;
}

At the point when the above code is arranged and executed, it creates the accompanying result:

Pointers to Structures

You can characterize pointers to structures in very much alike route as you characterize pointer to whatever other variable as takes after:

struct Books *struct_pointer;
Presently, you can store the location of a structure variable in the above-characterized pointer variable. To discover the location of a structure variable, put the & administrator before the structure's name as takes after:

struct_pointer = &Book1;
To get to the individuals from a structure utilizing a pointer to that structure, you must utilize the -> administrator as takes after:

struct_pointer->title;
Give us a chance to re-compose above sample utilizing structure pointer, trust this will be simple for you to comprehend the idea:

----

Give us a chance to re-compose above sample utilizing structure pointer, trust this will be simple for you to comprehend the idea:

2013-09-14 04:38:13.942 demo[20745] Book title : Objective-C Programming
2013-09-14 04:38:13.942 demo[20745] Book author : Nuha Ali
2013-09-14 04:38:13.942 demo[20745] Book subject : Objective-C Programming Tutorial
2013-09-14 04:38:13.942 demo[20745] Book book_id : 6495407
2013-09-14 04:38:13.942 demo[20745] Book title : Telecom Billing
2013-09-14 04:38:13.942 demo[20745] Book author : Zara Ali
2013-09-14 04:38:13.942 demo[20745] Book subject : Telecom Billing Tutorial
2013-09-14 04:38:13.942 demo[20745] Book book_id : 6495700

Bit Fields

Bit Fields permit the pressing of information in a structure. This is particularly helpful when memory or information stockpiling is at a premium. Average illustrations:
  • Packing several objects into a machine word. e.g. 1 bit flags can be compacted.
  • Reading external file formats -- non-standard file formats could be read in. E.g. 9 bit integers.
Objective-C permits us do this in a structure definition by putting :bit length after the variable. Case in point:

struct packed_struct {
  unsigned int f1:1;
  unsigned int f2:1;
  unsigned int f3:1;
  unsigned int f4:1;
  unsigned int type:4;
  unsigned int my_int:9;
} pack;
Here, the packed_struct contains 6 individuals: Four 1 bit banners f1..f3, a 4 bit sort and a 9 bit my_int. 

Objective-C consequently packs the above bit fields as minimalistically as would be prudent, given that the most extreme length of the field is not exactly or equivalent to the whole number word length of the PC. In the event that this is not the situation, then a few compilers may permit memory cover for the fields whilst other would store the following field in the following word.

0 comments:

Post a Comment

My Instagram