个性化阅读
专注于IT技术分析

Objective-C使用结构体 – Objective-C开发教程

点击下载

上一章Objective-C开发教程请查看:Objective-C使用字符串

Objective-C数组允许你定义变量的类型,这些变量可以包含几个相同类型的数据项,但结构体是另一种用户定义的数据类型,在Objective-C编程中可用,它允许你组合不同类型的数据项。

OC的结构体和C语言的结构体一样,如果你有很好的C语言功底,可以跳过本章。

定义一个结构体

要定义结构体,必须使用struct语句。struct语句定义了一种新的数据类型,为你的程序定义了不止一个成员。结构语句的格式如下所示:

struct [structure tag] {
   member definition;
   member definition;
   ...
   member definition;
} [一个或多个结构体变量];  

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;      
   struct Books Book2;       
 
   Book1.title = @"算法设计";
   Book1.author = @"AAA"; 
   Book1.subject = @"算法分析与设计";
   Book1.book_id = 6495407;

   Book2.title = @"C++";
   Book2.author = @"BBB";
   Book2.subject = @"C++教程";
   Book2.book_id = 6495700;
 
   NSLog(@"Book 1 title : %@", Book1.title);
   NSLog(@"Book 1 author : %@", Book1.author);
   NSLog(@"Book 1 subject : %@", Book1.subject);
   NSLog(@"Book 1 book_id : %d", Book1.book_id);

   NSLog(@"Book 2 title : %@", Book2.title);
   NSLog(@"Book 2 author : %@", Book2.author);
   NSLog(@"Book 2 subject : %@", Book2.subject);
   NSLog(@"Book 2 book_id : %d", Book2.book_id);

   return 0;
}

作为函数参数的结构体

可以将结构体作为函数参数传递,其方式与传递任何其他变量或指针非常类似。访问结构体变量的方法与前面例子中的方法类似:

#import <Foundation/Foundation.h>

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

@interface SampleClass:NSObject
// 函数声明
- (void) printBook:( struct Books) book ;
@end

@implementation SampleClass 

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

@end

int main() {
   struct Books Book1;        
   struct Books Book2;        
 
   Book1.title = @"C++";
   Book1.author = @"AAAA"; 
   Book1.subject = @"C++编程";
   Book1.book_id = 6495407;

   Book2.title = @"高级算法";
   Book2.author = @"BBBB";
   Book2.subject = @"高级算法设计教程";
   Book2.book_id = 6495700;
 
   SampleClass *sampleClass = [[SampleClass alloc]init];
   [sampleClass printBook: Book1];

   [sampleClass printBook: Book2];

   return 0;
}

结构体指针

你可以像定义指向任何其他变量的指针一样定义指向结构体指针,如下所示

struct Books *struct_pointer;

现在,你可以将结构变量的地址存储在上述定义的指针变量中。要查找结构体变量的地址,请将&操作符放在结构名称之前,如下所示:

struct_pointer = &Book1;

要使用指向该结构的指针访问该结构的成员,必须使用->操作符,如下所示:

struct_pointer->title;

让我们用结构指针重写上面的例子,希望这对你理解这个概念会容易点:

Live Demo
#import <Foundation/Foundation.h>

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

@interface SampleClass:NSObject
- (void) printBook:( struct Books *) book ;
@end

@implementation SampleClass 
- (void) printBook:( struct Books *) book {
   NSLog(@"Book title : %@", book->title);
   NSLog(@"Book author : %@", book->author);
   NSLog(@"Book subject : %@", book->subject);
   NSLog(@"Book book_id : %d", book->book_id);
}

@end

int main() {
   struct Books Book1;        
   struct Books Book2;        
 
   Book1.title = @"OC编程";
   Book1.author = @"AAA"; 
   Book1.subject = @"OC编程教程";
   Book1.book_id = 6495407;

   Book2.title = @"C++编程";
   Book2.author = @"BBB";
   Book2.subject = @"C++编程教程";
   Book2.book_id = 6495700;
 
   SampleClass *sampleClass = [[SampleClass alloc]init];
   [sampleClass printBook:&Book1];

   [sampleClass printBook:&Book2];

   return 0;
}

结构体的位字段

位字段允许在结构中包装数据。当内存或数据存储非常昂贵时,这尤其有用。典型的例子−

  • 将几个对象装入一个机器字中。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..f3, 4位类型和9位my_int。

只要字段的最大长度小于或等于计算机的整数字长,Objective-C会自动将上述位字段压缩到尽可能小的范围内。如果不是这样,那么一些编译器可能会允许字段的内存重叠,而另一些编译器会在下一个字中存储下一个字段。

赞(0)
未经允许不得转载:srcmini » Objective-C使用结构体 – Objective-C开发教程

评论 抢沙发

评论前必须登录!