Objective C Tutorial - Objective C Structures






Objective-C can define structure by combining various type of variables.

Structures are used to represent a data value with several fields.

Syntax

To define a structure, use the struct statement.

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

The structureName is optional.

The following code defines a structure for a book.

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

The following code defines a structure for a student.

struct Student
{
   NSString *name;
   NSString *major;
   NSString *department;
   int      id;
} student;




Example

To access any member of a structure, use the dot operator ..

#import <Foundation/Foundation.h>

struct Books
{
   NSString *title;
   NSString *author;
   NSString *subject;
   int      id;
};
 
int main( )
{
   struct Books Book1;        /* Declare Book1 of type Book */
 
   Book1.title = @"Objective-C Programming";
   Book1.author = @"java2s.com"; 
   Book1.subject = @"Objective-C Programming Tutorial";
   Book1.id = 1;

   NSLog(@"Book title : %@\n", Book1.title);
   NSLog(@"Book author : %@\n", Book1.author);
   NSLog(@"Book subject : %@\n", Book1.subject);
   NSLog(@"Book id : %d\n", Book1.id);


   return 0;
}




Structures as Function Arguments

You can pass a structure as a function argument.

#import <Foundation/Foundation.h>

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

@interface SampleClass:NSObject

- (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 id : %d\n", book.id);
}
@end

int main( )
{
   struct Books Book1;        /* Declare Book1 of type Book */
 
   Book1.title = @"Objective-C Programming";
   Book1.author = @"java2s.com"; 
   Book1.subject = @"Objective-C Programming Tutorial";
   Book1.id = 1;

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

   return 0;
}

Pointers to Structures

You can define pointers to structures as follows:

struct Books *struct_pointer;

To store the address of a structure variable:

struct_pointer = &Book1;

To access the members of a structure using a pointer to that structure, use the -> operator as follows:

struct_pointer->title;
#import <Foundation/Foundation.h>

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

@interface SampleClass:NSObject

- (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 id : %d\n", book->id);
}
@end

int main( )
{
   struct Books Book1;        
 
   Book1.title = @"Objective-C Programming";
   Book1.author = @"java2s.com"; 
   Book1.subject = @"Objective-C Programming Tutorial";
   Book1.id = 1;
 
   SampleClass *sampleClass = [[SampleClass alloc]init];
   [sampleClass printBook:&Book1];


   return 0;
}