首页 > IOS > 结构体成员--Objective-C教程系统45

结构体成员--Objective-C教程系统45

来源:原创 作者:thomas 分类:IOS 阅读:877 日期:2015-03-14

Objective-C数组允许定义几个相同类型的数据项,而Objective-C结构体允许用户定义包含不同类型的数据项。

结构体通常用来表示一条记录,假设你想记录图书信息,你可能需要记录关于每本书的如下属性:

  • 书名    Title

  • 作者    Author

  • 主题    Subject

  • ID       Book ID

定义结构体


使用struct语句来定义一个结构体。结构体语句定义了一种新的数据类型,拥有一个或多个数据成员。结构体语句的格式如下:

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

structure tag 是可选的,每一个成员定义就是常用的变量定义形式,如int i; 或 float f; 或其他的变量定义语句。在结构体定义的结尾,分号结束之前,可以指定一个或多个可选的结构体变量。示例如下:

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

访问结构体成员


访问任意一个结构体成员,我们可以使用成员访问操作符(.)。成员访问操作符位于结构体变量名与将要访问的结构体成员变量名之间。你可以使用struct关键字来定义结构体类型的变量。示例如下:

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

编译并执行如上代码,输出结果如下:

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

结构体作为方法参数


你可以传递一个结构体作为方法参数,类似于传递其他变量或指针。示例如下:

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

编译并执行如上代码,输出结果如下:

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

结构体指针


你可以定义指向结构体的指针,类似于指向其他变量的指针:

struct Books *struct_pointer;

现在,你可以存储结构体变量的地址在上面定义的指针变量中。在结构体名称前放&操作符,可以获取结构体变量的内存地址:

struct_pointer = &Book1;

可以使用->操作符来访问指针变量的结构体成员:

struct_pointer->title;

使用结构体指针重写上面的示例:

#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 by passing address of Book1 */
    [sampleClass printBook:&Book1];

    /* print Book2 info by passing address of Book2 */ 
    [sampleClass printBook:&Book2];
    return 0; 
}

编译并执行如上代码,输出结果如下:

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

比特字段

比特字段允许包装数据到一个结构体中。当内存或数据存储空间比较小时特别有用。

  • 包装多个对象到机器字中。例如,包装1个比特位。

  • 读取扩展文件格式,非标准文件格式可以被读入,如9比特长整数。

Objective-C运行我们在结构体定义中放置(:比特长度)在变量后。例如:

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;

这里,packed_struct包含6个数据成员,4个1比特变量f1...f4,一个4比特变量,和1个9比特变量。

Objective-C自动包装上面的比特字段,提供的字段最大长度将少于或等于计算机的整数字长度。如果不是这种情况,一些编译器允许这些字段内存交叠,而其他编译器将存储下个字段到下个字中。

 

热门文章 更多>

微信扫一扫,关注技术十日谈