1 结构体 1.1 结构体定义 typedef struct { int year; int month; int day; }Date; 1.2 结构体的赋值 // 需要设置为公用属性 stu->_name = @"xgd"; // 需要对其进行类型转换,否则计算机不清楚后面的是数组还是结构体(本质就是将一个结构体赋值给另一个结构体) stu->_birthday = (Date){1999, 1 ,15}; // 对单一属性进行赋值 stu->_birthday.year = 1999; 2 对象 2.1 对象的定义 @interface Student : NSObject { @public // 设置为公用属性,以便对其进行相关操作 NSString *_name; Date _birthday; } @end 2.2 对象的创建 Student *stu = [Student new]; Student *student = [[Student alloc]init]; new和alloc/init在功能上几乎是一致的,分配内存并完成初始化。 区别: 1.new的方式只能采用默认的init方法完成初始化,采用alloc的方式可以用其他定制的初始化方法。 2.alloc分配内存的时候使用了zone,zone它是给对象分配内存的时候,把关联的对象分配到一个相邻的内存区域内,以便于调用时消耗很少的代价,提升了程序处理速度。 2.2 对象的方法 @implementation Student - (void)say{ NSLog(@"name = %@, year = %i, month = %i, day = %i", _name, _birthday.year, _birthday.month, _birthday.day); } @end